Identityserver4 声明未添加到Identity Server 4中的令牌

Identityserver4 声明未添加到Identity Server 4中的令牌,identityserver4,Identityserver4,我实现了Identity Service ProfileService以添加地址声明: public async Task GetProfileDataAsync(ProfileDataRequestContext context) { User user = await _userManager.GetUserAsync(context.Subject); ClaimsPrincipal principal = await _claimsFactory.CreateAsync(

我实现了Identity Service ProfileService以添加地址声明:

public async Task GetProfileDataAsync(ProfileDataRequestContext context) {
  
  User user = await _userManager.GetUserAsync(context.Subject);

  ClaimsPrincipal principal = await _claimsFactory.CreateAsync(user); 

  ClaimsIdentity identity = (ClaimsIdentity)principal.Identity;

  identity.AddClaims(
    new List<Claim> {
      new Claim(JwtClaimTypes.Address, "My Address")
    }
  );

  var claims = identity.Claims;

  context.AddRequestedClaims(identity.Claims);

}
然后,我通过添加两个作用域来更新SPA客户端:

IdentityServerConstants.StandardScopes.Address,
"subscription"
最后,在使用OIDClient.js时,我请求了这两个作用域

因为ASP.Net标识添加了这两个声明,所以我将在令牌上获取它们

但是如果ASP.NET Identity没有添加声明,那么如果我尝试在ProfileService中添加声明,它们不会添加到令牌中。知道为什么吗

我不知道IS ProfileService的用途,因为没有添加声明。

如果您的用户有地址声明,那么只要您在登录时包含地址范围,它就会自动添加到他们的配置文件中


编辑:如果所需声明与api资源关联,则不会在配置文件中返回,而是在access_令牌中返回。因此,如果您想查看用户的地址,则必须将其包含在标识资源中。

您应该将地址范围添加到客户端定义中允许的范围列表中:

    AllowedScopes = { 
      IdentityServerConstants.StandardScopes.OpenId,
      IdentityServerConstants.StandardScopes.Profile, 
      IdentityServerConstants.StandardScopes.Email, 
      IdentityServerConstants.StandardScopes.OfflineAccess,
      "api"
    },  

如果你能提供更多的细节,我会很乐意详细说明这个答案。我有点成功了,但我仍然有点困惑,没有在ProfileService中添加声明。知道为什么吗?我刚刚添加了一个更新。用户是否拥有请求的声明?如果用户没有声明,配置文件服务将不会将其添加到用户信息中。是的,用户有声明。但是当用户拥有声明并且我请求这些范围时,ProfileService就不需要将声明包含在令牌中。那么,您的意思是配置文件服务只是更改用户已经拥有和请求的声明值,而不是向令牌添加或删除声明?这令人困惑。您正在调用AddRequestedClaims。地址声明不是userinfo终结点的请求声明,它仅作为访问令牌创建的一部分被请求。如果您注意到GetProfileDataAsync是通过一个上下文调用的,该上下文可以是userinfo,也可以是其他内容。
IdentityServerConstants.StandardScopes.Address,
"subscription"
    AllowedScopes = { 
      IdentityServerConstants.StandardScopes.OpenId,
      IdentityServerConstants.StandardScopes.Profile, 
      IdentityServerConstants.StandardScopes.Email, 
      IdentityServerConstants.StandardScopes.OfflineAccess,
      "api"
    },