Chr*_*isW 15 c# asp.net-mvc azure asp.net-identity azure-active-directory
我正在使用Asp.net Identity(OWIN)构建ASP.NET MVC 5网站,并希望支持传统的用户名/密码身份验证以及针对Azure Active Directory的身份验证.此应用程序不需要针对Microsoft ID(Live ID),Facebook,Twitter或任何其他外部提供程序进行身份验证.我找到的最接近的SO问题是:如何在ASP.NET MVC上执行Azure Active Directory单点登录和表单身份验证
我查看了使用"个人用户帐户"选项以及VS 2015中的"工作和学校帐户"选项创建项目时创建的示例.我的身份验证工作正常; 只有当我尝试将它们结合起来时,我才遇到问题.
在我的Startup_Auth.cs文件中,我正在配置OWIN,如下所示:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
//app.UseCookieAuthentication(new CookieAuthenticationOptions { });
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
LoginPath = new PathString("/account/sign-in")
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false,
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
SecurityTokenValidated = (context) =>
{
return Task.FromResult(0);
},
AuthorizationCodeReceived = (context) =>
{
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
context.OwinContext.Response.Redirect("/Home/Error");
context.HandleResponse(); // Suppress the exception
return Task.FromResult(0);
}
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
此配置适用于密码身份验证,但不适用于AAD身份验证.要启用AAD身份验证,我需要注释掉设置AuthenticationType的行
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
Run Code Online (Sandbox Code Playgroud)
或者,只设置没有值的CookieAuthentication.
app.UseCookieAuthentication(new CookieAuthenticationOptions { });
Run Code Online (Sandbox Code Playgroud)
我猜这有一个相对简单的方法,并会欣赏一些关于从哪里开始寻找的想法.
我从微软搜索了例子。所有这些看起来都像您的解决方案。看这里:
另一个例子是这里WindowsAzureActiveDirectoryBearerAuthenticationOptions
| 归档时间: |
|
| 查看次数: |
4261 次 |
| 最近记录: |