Identityserver4 GetUserInfoAsync只返回sub,不返回其他声明

Identityserver4 GetUserInfoAsync只返回sub,不返回其他声明,identityserver4,Identityserver4,为什么GetUserInfoAsync只返回sub而不返回其他声明 var discoveryResponse = client.GetDiscoveryDocumentAsync("some Authorization Url").Result; var userInfoResponse = client.GetUserInfoAsync(new UserInfoRequest { Address = discoveryResponse.UserInfoEndpoint, To

为什么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\u令牌可能是这样的?因为声明在id_令牌中,但在这种情况下如何从GetUserInfoAsync返回声明

我的代码:

我在标识资源“电子邮件”列表中有:

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new List<IdentityResource>
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Email()
    };
}
在请求的范围内,我有:

scope: 'openid email'

UserInfo端点需要使用
access\u令牌
进行授权,该令牌至少具有
openid
作用域,该作用域被转换为
sub
声明作为响应。其余的都是可选的。那是按规定的

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

newAPIResource(“UserInfo”,new[]{JwtClaimTypes.Email,JwtClaimTypes.GivenName})
通过上面的代码,我们创建了ApiResource
“UserInfo”
,其作用域为
“UserInfo”
,以及一些相关的用户声明


同样,从第一手来看,你的假设是正确的,你必须引入一个
ApiResource
,里面有一个作用域和
email
声明,然后你将它放在你的
access\u令牌中,我已经将它添加到ApiResource列表、客户端和作用域中,但我仍然没有在/userinfo中。在ProfileService-GetProfileDataAsync中,我使用:
context.AddRequestedClaims(context.Subject.Claims)您添加了it服务器端,但您是否请求了新的范围?哇-您是对的-thx!!重要的是-声明必须在IdentityResource和ApiResource中。您还必须添加:JwtClaimTypes.Email,JwtClaimTypes.givename to IdentityResources是吗?这取决于您。这是当您始终使用
cludeuserclaimsinidtoken
时的身份令牌。在那里,您可以使用预定义的声明集,例如
IdentityResources.Email()
IdentityResources.Profile()
await _accessor.HttpContext.SignInAsync(subject, new Claim(ClaimNames.Email, email));
scope: 'openid email'