ASP.NET 5中使用AzureAD實(shí)現(xiàn)單點(diǎn)登錄
題記:在ASP.NET 5中雖然繼續(xù)可以沿用ASP.NET Identity來(lái)做驗(yàn)證授權(quán),不過(guò)也可以很容易集成支持標(biāo)準(zhǔn)協(xié)議的第三方服務(wù),比如Azure Active Directory。
其實(shí),在ASP.NET 5中集成AzureAD,利用其進(jìn)行驗(yàn)證和授權(quán),是非常簡(jiǎn)單的。因?yàn)椋菏紫華zure 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項(xiàng)目中引用"Microsoft.AspNet.Authentication.OpenIdConnect"這個(gè)包,并正確配置AzureAD的連接信息,就可以很容易的進(jìn)行集成。
大致步驟如下:
1,在config.json文件中添加AzureAD的配置信息:
"AzureAd": {
"ClientId": "[Enter the clientId of your application as obtained from portal, e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
"Tenant": "[Enter the name of your tenant, e.g. contoso.onmicrosoft.com]",
"AadInstance": "https://login.microsoftonline.com/{0}", // This is the public instance of Azure AD
"PostLogoutRedirectUri": https://localhost:44322/
}
2,修改project.json,引入OpenIdConnect的中間件:
"Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-*"
3,在Startup中的ConfigureServices方法里面添加:
// OpenID Connect Authentication Requires Cookie Auth
services.Configure<ExternalAuthenticationOptions>(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
4,在Startup中的Configure方法里面添加:
// Configure the OWIN Pipeline to use Cookie Authentication
app.UseCookieAuthentication(options =>
{
// By default, all middleware are passive/not automatic. Making cookie middleware automatic so that it acts on all the messages.
options.AutomaticAuthentication = true;
});
// Configure the OWIN Pipeline to use OpenId Connect Authentication
app.UseOpenIdConnectAuthentication(options =>
{
options.ClientId = Configuration.Get("AzureAd:ClientId");
options.Authority = String.Format(Configuration.Get("AzureAd:AadInstance"), Configuration.Get("AzureAd:Tenant"));
options.PostLogoutRedirectUri = Configuration.Get("AzureAd:PostLogoutRedirectUri");
options.Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed,
};
});
5,Startup的OnAuthenticationFailed方法為:
private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
{
notification.HandleResponse();
notification.Response.Redirect("/Home/Error?message=" + notification.Exception.Message);
return Task.FromResult(0);
}
6,添加一個(gè)名為AccountController的Controller:
public class AccountController : Controller
{
// GET: /Account/Login
[HttpGet]
public IActionResult Login()
{
if (Context.User == null || !Context.User.Identity.IsAuthenticated)
return new ChallengeResult(OpenIdConnectAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });
return RedirectToAction("Index", "Home");
}
// GET: /Account/LogOff
[HttpGet]
public IActionResult LogOff()
{
if (Context.User.Identity.IsAuthenticated)
{
Context.Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationScheme);
Context.Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationScheme);
}
return RedirectToAction("Index", "Home");
}
}
以上代碼也可以到我Fork的完整示例項(xiàng)目中找到:https://github.com/heavenwing/WebApp-OpenIdConnect-AspNet5
【更新:2015-07-16】
如果你遇到添加了 [Authorize] ,但是不能自動(dòng)轉(zhuǎn)到登錄頁(yè)面的情況,那么需要:
app.UseOpenIdConnectAuthentication(options => {
options.AutomaticAuthentication = true;
});
具體見(jiàn):https://github.com/aspnet/Security/issues/357#issuecomment-120834369
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
asp.net 獲取目錄下的文件數(shù)和文件夾數(shù)
遍歷一個(gè)文件夾中的文件,需要用到DirectoryInfo類(lèi)中的一個(gè)重要的方法GetFileSystemInfos(),此方法返回指定的是與搜索條件相匹配的文件和子目錄的強(qiáng)類(lèi)型 FileSystemInfo對(duì)象的數(shù)組。2010-07-07
使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移
這篇文章介紹了使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
使用updatepanel局部刷新實(shí)現(xiàn)注冊(cè)時(shí)對(duì)用戶(hù)名的檢測(cè)示例
這篇文章主要介紹了使用updatepanel局部刷新實(shí)現(xiàn)注冊(cè)時(shí)對(duì)用戶(hù)名的檢測(cè)示例,需要的朋友可以參考下2014-03-03
ASP.NET從客戶(hù)端中檢測(cè)到有潛在危險(xiǎn)的request.form值的3種解決方法
這篇文章主要介紹了ASP.NET從客戶(hù)端中檢測(cè)到有潛在危險(xiǎn)的request.form值的3種解決方法,這是ASP.NET開(kāi)發(fā)中一個(gè)比較常見(jiàn)的經(jīng)典的問(wèn)題,需要的朋友可以參考下2015-01-01
ASP.NET動(dòng)態(tài)添加用戶(hù)控件的方法
這篇文章主要介紹了ASP.NET動(dòng)態(tài)添加用戶(hù)控件的方法,涉及asp.net用戶(hù)控件的動(dòng)態(tài)創(chuàng)建與使用技巧,需要的朋友可以參考下2015-07-07
.NET Core結(jié)合Nacos實(shí)現(xiàn)配置加解密的方法
當(dāng)我們把應(yīng)用的配置都放到配置中心后,很多人會(huì)想到這樣一個(gè)問(wèn)題,配置里面有敏感的信息要怎么處理呢?本文就詳細(xì)的介紹了.NET Core Nacos配置加解密,感興趣的可以了解一下2021-06-06
使用 Salt + Hash 將密碼加密后再存儲(chǔ)進(jìn)數(shù)據(jù)庫(kù)
如果你需要保存密碼(比如網(wǎng)站用戶(hù)的密碼),你要考慮如何保護(hù)這些密碼數(shù)據(jù),象下面那樣直接將密碼寫(xiě)入數(shù)據(jù)庫(kù)中是極不安全的,因?yàn)槿魏慰梢源蜷_(kāi)數(shù)據(jù)庫(kù)的人,都將可以直接看到這些密碼2012-12-12

