日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

OAuth的MVC实现(微软)

發布時間:2024/4/14 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OAuth的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);}

  

轉載于:https://www.cnblogs.com/panpanwelcome/p/7682832.html

總結

以上是生活随笔為你收集整理的OAuth的MVC实现(微软)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。