AspNet Core Identity GetExternalLoginInfoAsync 始终为空

Dan*_* L. 5 c# asp.net-identity entity-framework-core .net-core identityserver4

我正在使用 IdentityServer4 和 ASP .NET Core Identity 的组合来创建联合登录页面。我使用的外部提供程序之一是通过 Open ID Connect 协议的 Azure Active Directory。我还为我的所有数据存储集成了 EntityFrameworkCore。

在我使用身份验证搭建初始网站后,我将适当的服务添加到我的Startup.cs文件中。

services.AddOidcStateDataFormatterCache();
// Some DI registrations
services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<MyDbContext>();
services.AddMvc().AddRazorPages(options => /* Some options */);
services.AddIdentityServer(options =>
    options.Events.RaiseErrorEvents = true;
    options.Events.RaiseFailureEvents = true;
    options.Events.RaiseInformationEvents = true;
    options.Events.RaiseSuccessEvents = true;
)
.AddConfigurationStore(options => /* Set up DbContext */)
.AddOperationStore(options => /* Set up DbContext */)
.AddAspNetIdentity<MyAppUser>();
services.AddAuthentication().AddOpenIdConnect(options =>
{
    // Does not bind itself for mysterious reasons
    Configuration.GetSection("OpenIdConect").Bind(options)
});
Run Code Online (Sandbox Code Playgroud)

我决定如果我在我的appsettings.json文件中进行了大部分身份验证设置,它看起来会更好。

{
    "OpenIdConnect": {
        "ClientId": "<my_client_id>",
        "Authority:" "https://login.microsoft.com/<my_tenant_id>",
        "PostLogoutRedirectUri": "http://localhost:5000/MyApp/Account",
        "CallbackPath": "/signin-oidc",
        "ResponseType": "code id_token",
        "Scope": "openid profile email",
        "SignInScheme": "idsrv.external",
        "AutomaticAuthenticate": "false",
        "AutomaticChallenge": "false",
        "RequireHttpsMetadata": "true"
     }
}
Run Code Online (Sandbox Code Playgroud)

一切都在运行,我的 Open ID Connect 登录提供程序会自动出现在登录页面上。粉碎!在尝试使用它时,我遇到了一个错误:Error loading external login information.在脚手架上的 Razor 页面上 Areas/Identity/Pages/Account/ExternalLogin.cshtml.cs:72有这么一段代码:

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)

info总是解析为空。我怀疑这是我的Startup.cs.

Dan*_* L. 5

绞尽脑汁后,我决定重新阅读有关外部身份提供商的 IdentityServer4 文档。

\n\n

这引起了我(疲惫的)的注意:

\n\n
\n

鉴于这是一种常见的做法,IdentityServer 专门为此外部提供者工作流程注册了一个 cookie 处理程序。该方案通过 \n 常量表示 IdentityServerConstants.ExternalCookieAuthenticationScheme

\n\n

如果您要使用我们的外部 cookie 处理程序,则对于 SignInScheme [在您的 OpenIdConnect 服务设置中],您\xe2\x80\x99d 将值指定为IdentityServerConstants.ExternalCookieAuthenticationScheme常量

\n
\n\n

好吧,我想我没有使用他们的外部 cookie 处理程序。很确定我会让 ASP .NET Core Identity 来做这件事。我决定在 appsettings.json 中删除这一行:

\n\n
"SignInScheme": "idsrv.external"\n
Run Code Online (Sandbox Code Playgroud)\n\n

看哪,我可以使用我的 Active Directory 凭据登录。阅读 的实际源代码GetExternalLoginInfoAsync,我发现他们正在寻找一个非常具体的 cookie,但它不是"idsrc.external"。我需要将其保留为默认值。

\n