我正在使用 Asp.net Identity (OWIN) 构建 ASP.NET MVC 5 网站,并希望支持传统的用户名/密码身份验证以及针对 Azure Active Directory 的身份验证。此应用程序不需要针对 Microsoft ID(Live ID)、Facebook、Twitter 或任何其他外部提供商进行身份验证。我发现的最接近的问题是这个:How to do both Azure Active Directory Single Sign On and Forms Authentications on ASP.NET MVC
我查看了使用 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);
}
}
}
);
}
此配置适用于密码身份验证,但不适用于 AAD 身份验证。要启用 AAD 身份验证,我需要注释掉设置 AuthenticationType 的行
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
或者,只需设置不带任何值的 CookieAuthentication。
app.UseCookieAuthentication(new CookieAuthenticationOptions { });
我猜想有一个相对简单的方法可以实现这一点,并且希望能提供一些关于从哪里开始寻找的想法。
最佳答案
我从微软搜索了示例。所有这些看起来都像您的解决方案。看这里:
另一个例子是here与 WindowsAzureActiveDirectoryBearerAuthenticationOptions
关于c# - 使用密码和 Azure Active Directory 身份验证的 Asp.net Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660780/