asp.net-core - ProfileDataRequestContext.RequestedClaimTypes 中缺少子声明

标签 asp.net-core identityserver4

在 IdentityServer4 中,我定义了一个包含一些声明的 IdentityResource:

new IdentityResource {
  Name = "userdata",
  UserClaims = new List<string> {
    JwtClaimTypes.Subject,
    JwtClaimTypes.Role,
    JwtClaimTypes.Email,
    JwtClaimTypes.Name
  }
}

然后将其添加到客户端配置中的范围:

AllowedScopes = {
  IdentityServerConstants.StandardScopes.OpenId,
  "api1",
  "userdata"
}

在客户端应用程序中,我请求了该范围:

.AddOpenIdConnect("oidc", options => {
  options.Scope.Add("openid");
  options.Scope.Add("userdata");
  options.Scope.Add("offline_access");
  options.Scope.Add("api1");
}

在 IS4 中,我实现了 IProfileService 来向 id_token 添加一些声明。

public async Task GetProfileDataAsync(ProfileDataRequestContext context) {

  var sub = context.Subject.GetSubjectId();
  var user = await _userManager.FindByIdAsync(sub);
  if (user == null) {
    throw new ArgumentException("");
  }

  var principal = await _claimsFactory.CreateAsync(user);
  var userClaims = principal.Claims.ToList();

  var claims = new List<Claim> {
    userClaims.Find(x => x.Type == JwtClaimTypes.Subject),
    userClaims.Find(x => x.Type == JwtClaimTypes.Role),
    userClaims.Find(x => x.Type == JwtClaimTypes.Email),
    userClaims.Find(x => x.Type == JwtClaimTypes.Name),
    new Claim("iesseapetenantid", user.TenantId.ToString())
  };

  var requestedClaimTypes = context.RequestedClaimTypes;

  context.AddRequestedClaims(claims);
}

context.AddRequestedClaims(claims) 过滤传递的声明并仅添加客户端请求的声明。

问题在于 context.RequestedClaimTypes 中缺少 sub 声明,但其他三个声明却存在:电子邮件姓名角色

enter image description here

为什么缺少“sub”声明?

谢谢!

最佳答案

sub 声明是spec 所要求的声明。 ,因此它是 StandardScopes.OpenId 的一部分。您无需额外设置或请求。

var sub = context.Subject.GetSubjectId();

假设sub声明已经在上下文中。如果没有,就会抛出异常。

以上都是关于IdentityResourceid_token。可以在不引用用户的情况下创建 ApiResource token

关于asp.net-core - ProfileDataRequestContext.RequestedClaimTypes 中缺少子声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217254/

相关文章:

asp.net - 将 OpenIDConnect 集成到经典 ASP 应用程序中

asp.net - 找不到 'aspnetcorev2_inprocess.dll' 。异常消息

authentication - 如何使用 Identity Server 授权 Blazor WebAssembly SPA 应用程序

azure - 创建独立部署时如何解决 Azure Devops Pipeline 中的目标问题

c# - IHeadersDictionary 在模拟后返回 null

asp.net-core - 从访问 token Web Api Asp.net Core 获取用户信息

c# - 用户已通过身份验证,但访问 token 在哪里?

asp.net-identity - 如何使用 Identity Server 实现 SSO?

c# - 将 View 渲染为字符串缓存问题

angular - 使用 Angular 和 ASP.NET Core 将 API 调用重定向到 Azure 上的根 URL