麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

ASP.NET 5中使用AzureAD實現(xiàn)單點登錄

2024-07-10 13:29:12
字體:
供稿:網(wǎng)友

本文給大家介紹的是在ASP.NET 5中使用AzureAD實現(xiàn)單點登錄的方法和示例,有需要的小伙伴可以參考下。

題記:在ASP.NET 5中雖然繼續(xù)可以沿用ASP.NET Identity來做驗證授權(quán),不過也可以很容易集成支持標(biāo)準(zhǔn)協(xié)議的第三方服務(wù),比如Azure Active Directory。

其實,在ASP.NET 5中集成AzureAD,利用其進行驗證和授權(quán),是非常簡單的。因為:首先Azure Active Directory提供了OAuth2.0、OpenId Connect 1.0、SAML和WS-Federation 1.2標(biāo)準(zhǔn)協(xié)議接口;其次微軟在ASP.NET 5中移植了集成OpenId Connect的OWIN中間件。所以,只要在ASP.NET 5項目中引用"Microsoft.AspNet.Authentication.OpenIdConnect"這個包,并正確配置AzureAD的連接信息,就可以很容易的進行集成。

大致步驟如下:

1,在config.json文件中添加AzureAD的配置信息:

 

 
  1. "AzureAd": { 
  2. "ClientId""[Enter the clientId of your application as obtained from portal, e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]"
  3. "Tenant""[Enter the name of your tenant, e.g. contoso.onmicrosoft.com]"
  4. "AadInstance""https://login.microsoftonline.com/{0}", // This is the public instance of Azure AD 
  5. "PostLogoutRedirectUri": https://localhost:44322/ 

2,修改project.json,引入OpenIdConnect的中間件:

 

 
  1. "Microsoft.AspNet.Authentication.OpenIdConnect""1.0.0-*" 

3,在Startup中的ConfigureServices方法里面添加:

 

 
  1. // OpenID Connect Authentication Requires Cookie Auth 
  2. services.Configure<ExternalAuthenticationOptions>(options => 
  3. options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
  4. }); 

4,在Startup中的Configure方法里面添加:

 

 
  1. // Configure the OWIN Pipeline to use Cookie Authentication 
  2. app.UseCookieAuthentication(options =>  
  3. // By default, all middleware are passive/not automatic. Making cookie middleware automatic so that it acts on all the messages. 
  4. options.AutomaticAuthentication = true
  5.  
  6. }); 
  7.  
  8. // Configure the OWIN Pipeline to use OpenId Connect Authentication 
  9. app.UseOpenIdConnectAuthentication(options => 
  10. options.ClientId = Configuration.Get("AzureAd:ClientId"); 
  11. options.Authority = String.Format(Configuration.Get("AzureAd:AadInstance"), Configuration.Get("AzureAd:Tenant")); 
  12. options.PostLogoutRedirectUri = Configuration.Get("AzureAd:PostLogoutRedirectUri"); 
  13. options.Notifications = new OpenIdConnectAuthenticationNotifications 
  14. AuthenticationFailed = OnAuthenticationFailed, 
  15. }; 
  16. }); 

5,Startup的OnAuthenticationFailed方法為:

 

 
  1. private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) 
  2. notification.HandleResponse(); 
  3. notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message); 
  4. return Task.FromResult(0); 

6,添加一個名為AccountController的Controller:

 

 
  1. public class AccountController : Controller 
  2. // GET: /Account/Login 
  3. [HttpGet] 
  4. public IActionResult Login() 
  5. if (Context.User == null || !Context.User.Identity.IsAuthenticated) 
  6. return new ChallengeResult(OpenIdConnectAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" }); 
  7. return RedirectToAction("Index""Home"); 
  8.  
  9. // GET: /Account/LogOff 
  10. [HttpGet] 
  11. public IActionResult LogOff() 
  12. if (Context.User.Identity.IsAuthenticated) 
  13. Context.Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationScheme); 
  14. Context.Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationScheme); 
  15. return RedirectToAction("Index""Home"); 

以上代碼也可以到我Fork的完整示例項目中找到:https://github.com/heavenwing/WebApp-OpenIdConnect-AspNet5

【更新:2015-07-16】

如果你遇到添加了 [Authorize] ,但是不能自動轉(zhuǎn)到登錄頁面的情況,那么需要:

 

 
  1. app.UseOpenIdConnectAuthentication(options => { 
  2. options.AutomaticAuthentication = true
  3. }); 

具體見:https://github.com/aspnet/Security/issues/357#issuecomment-120834369

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久av喷吹av高潮av懂色 | 在线日韩亚洲 | 久久精品亚洲精品国产欧美kt∨ | 99成人精品视频 | 久草免费资源视频 | 久久福利小视频 | 国产免费观看电影网站 | 欧美日韩精品一区二区三区不卡 | 欧美一级特黄aaaaaa在线看首页 | 色综合精品 | 一级黄色影院 | 欧美成人一区二区视频 | 中文字幕在线观看视频www | 国产精品久久久久国产精品三级 | 欧美色大成网站www永久男同 | 亚洲第五色综合网 | 日本看片一区二区三区高清 | 久久久成人一区二区免费影院 | 日韩av片在线播放 | 牛牛视频在线 | 中文字幕网址 | 粉嫩粉嫩一区二区三区在线播放 | 久久久久亚洲美女啪啪 | 国产亚洲网 | 毛片视频免费播放 | 免费看一级片 | 国产精品久久久久久久av | 国产一区成人 | 欧美一级在线免费 | 中文字幕涩涩久久乱小说 | 激情av在线 | 中文字幕一二三区芒果 | 人人看人人舔 | 欧美亚洲综合网 | 免费男女乱淫真视频 | 毛片免费在线观看视频 | 成人爽a毛片免费啪啪红桃视频 | 国产精品久久久久av | 一边吃奶一边摸下娇喘 | 91精品观看91久久久久久国产 | 污视频在线免费 |