IdentityServer4与ASP.NET Core 2.1 Identity - 加载外部登录信息时出错

4
我正在尝试使用自己的UserStore为SSO使IdentityServer4与ASP.NET Core Identity配合工作。虽然指南似乎非常简单,并且身份验证过程本身似乎有效,但在应用程序(另一个ASP.NET Core MVC应用程序)中,我遇到了以下错误:Error loading external login information
我的设置如下:
对于ASP.NET MVC应用程序(客户端):
services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies", options =>
{
    options.ExpireTimeSpan =TimeSpan.FromMinutes(30);
})
.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";

    options.Authority = "https://localhost:5001/";

    options.ClientId = "clientId";
    options.ClientSecret = "secret";
    options.SaveTokens = true;
    options.ResponseType = "code id_token";
    options.Scope.Add(IdentityServerConstants.StandardScopes.Profile);
    options.Scope.Add(IdentityServerConstants.StandardScopes.Email);
    options.Scope.Add(IdentityServerConstants.StandardScopes.OfflineAccess);

    options.GetClaimsFromUserInfoEndpoint = true;
});

对于 IdentityServer4 应用程序:
services.AddScoped<UserManager<User>, MyUserManager>();
services.AddIdentity<User, UserGroup>()
    .AddRoleStore<MyRoleStore>()
    .AddUserStore<MyUserStore>()
    .AddDefaultTokenProviders();

services.Configure<IdentityOptions>(options =>
{
    options.ClaimsIdentity.UserIdClaimType = JwtClaimTypes.Subject;
    options.ClaimsIdentity.UserNameClaimType = JwtClaimTypes.Name;
    options.ClaimsIdentity.RoleClaimType = JwtClaimTypes.Role;
});

services.AddIdentityServer()
    .AddDeveloperSigningCredential()
    .AddInMemoryApiResources(OpenIDConfig.GetApiResources())
    .AddInMemoryIdentityResources(OpenIDConfig.GetIdentityResources())
    .AddInMemoryClients(OpenIDConfig.GetClients())
    .AddResourceOwnerValidator<ResourceOwnerPasswordValidator<User>>()
    .AddProfileService<ProfileService<User>>();

主要问题是我不知道从哪里开始查找为什么在成功身份验证后会出现问题。


你能添加ProfileService的代码吗?那是我开始查找的地方。 - user4864425
@DanielRusznyak 我基本上有同样的问题,使用Google作为外部身份提供者。你解决了吗? - Thomas Levesque
2
@ThomasLevesque,这不适用于IS4,但我发现Google存在类似的问题,问题在于在某些平台和浏览器中,必须禁用外部cookie的SameSite,因为“Lax”不受良好支持。 services.ConfigureExternalCookie(options => { options.Cookie.SameSite = SameSiteMode.None; }); - Kanadaj
4
实际上问题在于我改变了Google选项中的默认“SignInScheme”。这必须是“IdentityConstants.ExternalScheme”,因为这是“SignInManager.GetExternalLoginInfoAsync”使用的内容。 - Thomas Levesque
@ThomasLevesque 谢谢你。我一直在遵循 IDS$ 文档 (https://docs.identityserver.io/en/release/quickstarts/4_external_authentication.html) ,他们使用 options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme; 但这并不起作用。然而,你的选项却可以! - Marko Prcać
显示剩余2条评论
1个回答

5

由于这篇评论对我很有帮助,但我差点错过了答案,所以我将它放在这里。这篇评论是由@Thomas Levesque发表的,你可能要感谢他的回答 ;-)

实际上问题出在我改变了Google选项中的默认SignInScheme。它必须是IdentityConstants.ExternalScheme,因为这是SignInManager.GetExternalLoginInfoAsync使用的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接