asp.net-core - 如何使用 Identityserver4 从外部提供商获取个人资料图片?

标签 asp.net-core asp.net-identity .net-core profile identityserver4

我正在将 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/

相关文章:

asp.net-core - 将 MVC 5 迁移到 MVC 6

c# - ASP.NET Core 中 UseHttpsRedirection 和 AddDirectToHttpsPermanent 的区别

asp.net-mvc - 如何使用 StructureMap 配置 ASP.NET Identity ApplicationUserManager

asp.net-identity - 将用户从表导入到 asp.net 标识 AspNetUsers 表?

c# - 在 Asp.Net Core 3.0 中禁用身份验证以进行开发

asp.net-core - 如何在 .NET Core 2.0 中获取登录用户的用户 ID?

azure - Api Post 方法在.net core azure 生产中不起作用

c# - UserStore 每个 HttpRequest 的 Unity IoC 生命周期

exception-handling - 点网核心 : OnException method in a custom filter is called two times

asp.net-core - 无法运行 dnx 控制台应用程序