在 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 声明,但其他三个声明却存在:电子邮件、姓名和角色。
为什么缺少“sub”声明?
谢谢!
最佳答案
sub
声明是spec 所要求的声明。 ,因此它是 StandardScopes.OpenId
的一部分。您无需额外设置或请求。
var sub = context.Subject.GetSubjectId();
假设sub
声明已经在上下文中。如果没有,就会抛出异常。
以上都是关于IdentityResource
和id_token
。可以在不引用用户的情况下创建 ApiResource
的 token
。
关于asp.net-core - ProfileDataRequestContext.RequestedClaimTypes 中缺少子声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217254/