gpt4 book ai didi

identityserver4 - GetUserInfoAsync 仅返回 sub 而没有其他声明

转载 作者:行者123 更新时间:2023-12-04 17:34:39 25 4
gpt4 key购买 nike

为什么 GetUserInfoAsync 只返回 sub 而没有其他声明?

var discoveryResponse = client.GetDiscoveryDocumentAsync("some Authorization Url").Result;
var userInfoResponse = client.GetUserInfoAsync(new UserInfoRequest
{
Address = discoveryResponse.UserInfoEndpoint,
Token = token // access_token
}).Result;

登录后,我在响应中有“电子邮件”,但当我调用 GetUserInfoAsync 时却没有。我可能会传递给 GetUserInfoAsync access_token?因为声明在 id_token 中,但在这种情况下我如何从 GetUserInfoAsync 返回声明?

我的代码:

我在 IdentityResource 的“电子邮件”列表中有:

public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Email()
};
}

在客户端我有“电子邮件”和“AlwaysIncludeUserClaimsInIdToken”:

return new Client
{
ClientId = clientId,
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "some url" },
PostLogoutRedirectUris = { "some url" },
AlwaysIncludeUserClaimsInIdToken = true,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
"email"
}
};

我在 SignInAsync 方法中传递范围:

await _accessor.HttpContext.SignInAsync(subject, new Claim(ClaimNames.Email, email));

在我请求的范围内:

scope: 'openid email'

最佳答案

UserInfo 端点需要使用至少具有 openid 范围的 access_token 进行授权,该范围被转换为 sub 声明作为响应。其余的都是可选的。即由spec .

现在让我们see这是如何在 IdentityServer 4 中安排的。与 access_token 相关的所有内容(旨在由 API 使用)都分组到 ApiResource 配置中。这是唯一可以配置 API 范围 及其声明的地方。引入范围后,您可以将其添加到特定客户端的可访问列表中。然后,客户端你可以明确地请求它。 ApiResource 配置可能看起来有点乱,因为它有额外的字段,例如用于 Introspection 端点访问的 API 凭据,但我们需要获取一些 UseInfo 数据的构造函数非常简单:

new ApiResource("UserInfo", new []{JwtClaimTypes.Email, JwtClaimTypes.GivenName})

使用上面的代码,我们创建了 ApiResource "UserInfo",范围为 "UserInfo" 和一些相关的用户声明。

都一样,更多,来自第一手here

关于identityserver4 - GetUserInfoAsync 仅返回 sub 而没有其他声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57164645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com