首页 > 解决方案 > 将 ASP.Net Core 2 Identity 与 Azure Ad 单租户身份验证结合使用

问题描述

对于一个我不理解的问题,我想从社区获得帮助。我创建了 asp.net core 2 web 应用程序,我想将应用程序配置为能够通过 aspnetuser 表或使用O365 公司帐户从应用程序登录。然后我遵循了 MSDN 网站上包含的网络上描述的多种技术。应用身份验证工作正常,但 Azure 添加返回:加载外部登录信息时出错。 我通过生成身份视图检查了代码内部,应用程序失败了:

 var info = await _signInManager.GetExternalLoginInfoAsync();
        if (info == null)
        {
            ErrorMessage = "Error loading external login information.";
            return RedirectToPage("./Login", new { ReturnUrl = returnUrl });
        }

等待 _signInManager.GetExternalLoginInfoAsync(); 返回 null 并返回错误信息。

该应用程序已在 azure AD 中正确配置,如果我从应用程序中删除身份验证,它可以在我的应用程序中运行。

我将我的应用程序中间件配置如下:

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.AddAuthentication(AzureADDefaults.AuthenticationScheme).AddCookie()
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));
        services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";
            options.TokenValidationParameters.ValidateIssuer = true;
        });

在配置方法中我添加了

app.UseAuthentication();

当我到达我的登录屏幕应用程序(由 VS 搭建)时,一切似乎都是正确的:

具有两种身份验证可能性的登录屏幕]:

具有两种身份验证可能性的登录屏幕

当我尝试 Azure Active Directory 方法时出现错误消息:

当我尝试 Azure Active Directory 方法时出现错误消息

有人可以解释并帮助我解决这个问题吗?

提前致谢

标签: asp.net-coreasp.net-identityazure-active-directory

解决方案


解决方案是将 cookieschemename 添加为 externalscheme。下面是 Startup.cs 文件中的示例代码块。

 services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => { Configuration.Bind("AzureAd", options); options.CookieSchemeName = IdentityConstants.ExternalScheme; });


推荐阅读