![](/img/trans.png)
[英]Azure AD authentication to existing ASP.NET Core Identity application
[英].Net Core Identity and Azure AD Authentication
我有一个网络核心网络应用程序,在设置和工作时使用网络核心身份登录,它创建并登录存储在我们数据库中的用户。 我的下一步是添加Azure Active Directory作为外部登录方式,也可以正常工作,在大多数情况下,我能够登录。
我的问题是,当我添加AAD身份验证时,身份验证的身份方式不再适用于签到我。
理想情况下,Web应用程序将使用ADD对用户进行身份验证,但如果失败,用户仍然可以选择在本地登录以进行身份验证。 基本上ADD将是默认登录,身份将是备份。
我已经按照以下帖子的建议,因为它与我希望我的网络应用程序做的非常相似,通过将AddCookie()添加到Startup.cs文件,但是当我这样做时,ADD无法验证我随着消息:
“我们无法让你进去。请再试一次。”
.net核心中的混合身份验证与Open Id Connect和本地数据库
以下是我的Startup.cs文件的样子,我从上面的帖子中删除了AddCookies()调用,所以我可以让ADD再次登录。
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
options.Authority = options.Authority + "/v2.0/"; // Microsoft identity platform
options.TokenValidationParameters.ValidateIssuer = false; // accept several tenants (here simplified)
});
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
我怀疑它可能与以下调用有关:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
也许我需要添加额外的身份验证选项? 我尝试了以下但ADD没有验证我,我从ADD收到相同的消息:
“我们无法让你进去。请再试一次。”
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = AzureADDefaults.AuthenticationScheme;
})
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
我对认证很新,感谢任何帮助,谢谢。
我想你只需要注册两个认证方案。
交换此代码:
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder(
AzureADDefaults.AuthenticationScheme,
AzureADDefaults.OpenIdScheme)
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.