我正在将 Identityserver4 与 Net Core 结合使用。我可以使用 Google、Facebook、Twitter、LinkedIn 和 Microsoft 进行身份验证,但现在我想从外部提供商获取用户的个人资料图片。我正在使用具有 GrantTypes.Implicit 的客户端,并尝试添加新的 IdentityResource,如下所示:
new IdentityResource {
Name = "picture",
UserClaims = new List<string> {"picture" }
}
然后我将“图片”添加为我的客户的允许范围。不过,这一切都是在黑暗中进行的,因为我仍在努力了解身份资源和允许的范围。
由于我对 Identityserver4 非常陌生,请友善并尽可能详细。提前致谢!
最佳答案
身份资源中应该已经有一个名为“个人资料”的“范围”,具有一组用户声明 - 其中一个是“图片”。
由于您使用的是 ASP.NET Identity,因此您需要将任何给定用户的记录插入到 AspNetUserClaims 表中,声明类型为“图片”,其中值是在线图像的 URL。
已获得“个人资料”范围的客户端在向 UserInfo 端点发出请求时应收到此“图片”声明。
如果您想将该图片作为 id_token 的一部分,那么除了“个人资料”范围/资源之外,您还需要将“图片”声明添加到“openid”范围/资源。
(术语“范围”和“资源”在 Identity Server 数据模型中稍微混合在一起。)
--
要从 Google 获取图片声明,您可以执行以下操作:
info.Principal.Claims.Where(x => x.Type == "picture").Select(x => x.Value).FirstOrDefault()
--
使用 Microsoft.AspNetCore.Authentication.Google(即 app.UseGoogleAuthentication(google.Options(externalCookieScheme))
)时,Google 不会发送图片声明。
如果您改用 OIDC 方法,Google 会向您发送图片声明。
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc-google",
DisplayName = "Google",
Authority = "https://accounts.google.com",
ClientId = "",
ClientSecret = "",
Scope = { "profile", "email", "openid" },
CallbackPath = "signin-google",
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role",
ValidateIssuer = false
}
});
关于asp.net-core - 如何使用 Identityserver4 从外部提供商获取个人资料图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357288/