【发布时间】:2021-10-27 17:12:01
【问题描述】:
在 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 声明,但其他三个声明存在:email、name 和 角色。
为什么缺少“sub”声明?
谢谢!
【问题讨论】:
-
如果只想添加一些声明,则无需实现 IProfileService。看看答案here。
-
@RuardvanElburg 在我的情况下,我需要实现
IProfileService,因为我想动态注入一些声明。谢谢。
标签: asp.net-core identityserver4