【问题标题】:.Net Core Identity and Azure AD Authentication.Net Core Identity 和 Azure AD 身份验证
【发布时间】:2019-08-29 21:09:47
【问题描述】:

我有一个 net core 网络应用程序,它设置并运行了 net core 身份登录,它创建并登录了存储在我们数据库中的用户。我的下一步是将 Azure Active Directory 添加为外部登录方式,而且效果也很好,在大多数情况下,我可以登录。

我的问题是,当我添加 AAD 身份验证时,身份验证方式不再让我登录。

理想情况下,Web 应用程序将使用 ADD 对用户进行身份验证,但如果失败,用户仍然可以选择在本地登录以进行身份​​验证。本质上,ADD 将是默认登录,身份将是备份。

我遵循了以下帖子的建议,因为它与我希望我的网络应用程序执行的操作非常相似,方法是将 AddCookie() 添加到 Startup.cs 文件,但是当我这样做时,添加失败使用消息对我进行身份验证:

“我们无法让您登录。请重试。”

Hybrid authentication in .net core with Open Id Connect and local database

以下是我的 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));

我对身份验证相当陌生,感谢您提供任何帮助。

【问题讨论】:

  • 如果 AAD 身份验证失败,是否要重定向回您的应用程序?或者您想解决 AAD 认证过程中的错误?
  • @NanYu - 最好返回 Web 应用程序,以便用户可以使用本地登录作为备份。使用下面 Paul 的代码解决了 AddCookie() 问题,除此之外,根据我已经能够测试的结果,ADD 身份验证工作正常,这是本地登录身份验证失败。
  • 你能解决这个问题吗?我正在尝试类似的东西并卡在同一个地方

标签: c# authentication asp.net-core azure-active-directory asp.net-identity


【解决方案1】:

我认为您只需要注册两个身份验证方案。

换成这段代码:

    services.AddMvc(options =>
    {
        var policy = new AuthorizationPolicyBuilder(
                AzureADDefaults.AuthenticationScheme, 
                AzureADDefaults.OpenIdScheme)
            .RequireAuthenticatedUser()
            .Build();
        options.Filters.Add(new AuthorizeFilter(policy));
    });

【讨论】:

  • 感谢您的回复。我已经尝试过了,本地登录仍然无法验证我的身份。我已经在 _LoginPartial 视图中调试了 if (User.Identity.IsAuthenticated) 和 if (SignInManager.IsSignedIn(User)) 并且都返回 false。
  • 这确实解决了添加 AddCookie() 调用时 ADD 无法验证的问题,但如上所述本地登录仍然无法验证。
猜你喜欢
  • 1970-01-01
  • 2021-01-03
  • 2017-06-26
  • 1970-01-01
  • 2020-03-09
  • 2021-03-16
  • 2020-07-27
  • 2017-09-11
  • 2020-06-16
相关资源
最近更新 更多