国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET 5中使用AzureAD實現單點登錄

2024-07-10 13:29:12
字體:
來源:轉載
供稿:網友

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

題記:在ASP.NET 5中雖然繼續可以沿用ASP.NET Identity來做驗證授權,不過也可以很容易集成支持標準協議的第三方服務,比如Azure Active Directory。

其實,在ASP.NET 5中集成AzureAD,利用其進行驗證和授權,是非常簡單的。因為:首先Azure Active Directory提供了OAuth2.0、OpenId Connect 1.0、SAML和WS-Federation 1.2標準協議接口;其次微軟在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] ,但是不能自動轉到登錄頁面的情況,那么需要:

 

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 康马县| 宜州市| 通山县| 凤冈县| 亚东县| 弋阳县| 曲阳县| 永康市| 五华县| 靖西县| 库尔勒市| 梁平县| 固原市| 永平县| 和政县| 浦县| 周口市| 香港| 来凤县| 临沂市| 清镇市| 新巴尔虎右旗| 平度市| 博客| 阳谷县| 彝良县| 斗六市| 红桥区| 合水县| 丰宁| 县级市| 漾濞| 乐亭县| 吴川市| 宽甸| 屯昌县| 工布江达县| 班玛县| 崇文区| 新沂市| 滨州市|