分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

OAuth的MVC实现(微软)

发布时间:2023-09-06 01:18责任编辑:沈小雨关键词:MVC

LoginController中:

第三方登陆

 ???????public ActionResult LogOn() ???????{ ???????????string liveUrl = ???????????????string.Format( ???????????????????"https://login.live.com/oauth20_authorize.srf?client_id={0}&scope=wl.Emails&response_type=code&redirect_uri={1}&locale={2}", ???????????????????this.ClientId, ???????????????????this.OAuthLogOnCallbackUrl, ???????????????????this.Locale); ???????????return this.Redirect(liveUrl); ???????}

登陆成功,获取授权 

 ???????public async Task<ActionResult> LogOnCallback() ???????{ ???????????string code = this.Request.QueryString["code"]; ???????????if (string.IsNullOrEmpty(code)) ???????????????return RedirectToAction("Index", "Login"); ???????????string tokenUrl = ???????????????string.Format( ???????????????????"https://login.live.com/oauth20_token.srf?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code&locale={4}", ???????????????????this.ClientId, ???????????????????this.OAuthLogOnCallbackUrl, ???????????????????this.ClientSecret, ???????????????????code, ???????????????????this.Locale); ???????????string liveId = string.Empty; ???????????try ???????????{ ???????????????liveId = await RequestLiveIdByToken(await RequestToken(tokenUrl)); ???????????} ???????????catch (Exception e) ???????????{ ???????????????_logger.Fatal("无法获取LiveId Token", e); ???????????????var result = new ViewModels.LoginResult ???????????????{ ???????????????????Success = false, ???????????????????ErrorMessage = "无法连接登录服务,请稍后再试。" ???????????????}; ???????????????return View("Index", result); ???????????} ???????????if (!string.IsNullOrEmpty(liveId)) ???????????{ ???????????????var userSvc = _userSvc; ???????????????if (userSvc.CurrentUser == null) ???????????????{ ???????????????????UserInfo user = userSvc.GetUserByEmail(liveId); ???????????????????if (user != null && user.IsEnable) ???????????????????{ ???????????????????????return this.DoLogin(user); ???????????????????} ???????????????????else ???????????????????{ ???????????????????????var result = new ViewModels.LoginResult ???????????????????????{ ???????????????????????????Success = false ???????????????????????}; ???????????????????????if (user != null && !user.IsEnable) ???????????????????????{ ???????????????????????????result.ErrorMessage = "用户被禁止登录!"; ???????????????????????} ???????????????????????else ???????????????????????{ ???????????????????????????result.ErrorMessage = "用户不存在!"; ???????????????????????} ???????????????????????return View("Index", result); ???????????????????} ???????????????} ???????????????return this.DoLogin(userSvc.CurrentUser); ???????????} ???????????return this.RedirectToAction("Index", "Login"); ???????} ???
 ???????[NonAction] ???????private async Task<string> RequestToken(string url) ???????{ ???????????var request = WebRequest.Create(url); ???????????using (var response = await request.GetResponseAsync()) ???????????{ ???????????????using (var sr = new StreamReader(response.GetResponseStream())) ???????????????{ ???????????????????var json = sr.ReadToEnd(); ???????????????????return JsonConvert.DeserializeAnonymousType(json, new { access_token = "" }).access_token; ???????????????} ???????????} ???????} ???????[NonAction] ???????private async Task<string> RequestLiveIdByToken(string token) ???????{ ???????????if (string.IsNullOrEmpty(token)) ???????????????return string.Empty; ???????????var request = WebRequest.Create(string.Format("https://apis.live.net/v5.0/me?access_token={0}", token)); ???????????using (var response = await request.GetResponseAsync()) ???????????{ ???????????????using (var sr = new StreamReader(response.GetResponseStream())) ???????????????{ ???????????????????string json = sr.ReadToEnd(); ???????????????????var userJson = JsonConvert.DeserializeAnonymousType(json, new { emails = new { account = "" } }); ???????????????????return userJson.emails.account; ???????????????} ???????????} ???????}

注销登陆 

 ???????public ActionResult LogOff() ???????{ ???????????this.PreLogout(); ???????????string liveUrl = ???????????????string.Format( ???????????????????"https://login.live.com/oauth20_logout.srf?client_id={0}&scope=wl.Emails&response_type=code&redirect_uri={1}&locale={2}", ???????????????????this.ClientId, ???????????????????this.OAuthLogOnCallbackUrl, ???????????????????this.Locale); ???????????return this.Redirect(liveUrl); ???????}

  

OAuth的MVC实现(微软)

原文地址:http://www.cnblogs.com/panpanwelcome/p/7682832.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved