将 ASP.Net Core 2 标识与 Azure Ad 单租户身份验证结合使用

Val*_*erf 3 asp.net-identity azure-active-directory asp.net-core

我想从社区获得帮助解决一个我不明白的问题。我创建了 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 });
        }
Run Code Online (Sandbox Code Playgroud)

等待 _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;
        });
Run Code Online (Sandbox Code Playgroud)

在配置方法中我添加了

app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

我尝试 Azure Active Directory 方法时的错误消息

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

提前致谢

小智 5

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

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

Run Code Online (Sandbox Code Playgroud)