题记:在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的配置信息:
"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" + notification.Exception.Message); return Task.FromResult(0); }
6,添加一个名为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的完整示例项目中找到:https://github.com/heavenwing/WebApp-OpenIdConnect-AspNet5
【更新:2015-07-16】
如果你遇到添加了 [Authorize] ,但是不能自动转到登录页面的情况,那么需要:
app.UseOpenIdConnectAuthentication(options => { options.AutomaticAuthentication = true; });
具体见:https://github.com/aspnet/Security/issues/357#issuecomment-120834369
以上所述就是本文的全部内容了,希望大家能够喜欢。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 国风大师纯音系列《古筝仙子 常静演奏 十指弹一》1CD[FLAC][919MB]
- DasKapital-OneMustHaveChaosInsidetoGiveBirthtoaDancingStar(2024)[24-44.1]WAV
- 王菲.1999-《只爱陌生人》日本东芝版[低速原抓WAV+CUE]
- 张小英1987-金奖金曲第一集[新加坡版][WAV+CUE]
- 鹿晗.2015-重启【海蝶】【FLAC分轨】
- 张芸京.2012-小女孩【金牌大风】【WAV+CUE】
- 余天.2002-雄狮魅力金曲2CD(南方金点系列)【南方】【WAV+CUE】
- 银霞.1982-《结·SAYYESMYBOY》台湾复刻版[WAV+CUE]
- 梁汉文1993-火热动感[引进版][WAV+CUE]
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】