【问题标题】:IdentityServer4 with AspNetIdentity - adding role claimsIdentityServer4 与 AspNetIdentity - 添加角色声明
【发布时间】:2020-08-20 12:30:27
【问题描述】:
我将 IdentityServerSPA 配置文件与 IdentityServer4 和 AspNetIdentity 一起使用。我想将 AspNetIdentity 中的角色作为角色声明包含在内。我可以在我的自定义 ProfileService 中看到角色声明是由 AspNetIdentity 添加到 ClaimPrincipal 中的,但它们没有添加到客户端应用程序中。我相信这是“AllowedScopes”的问题,但我尝试将这些添加到 appsettings.json 中的客户端配置中,但似乎没有任何效果。我确定我错过了一些东西,但我已经没有时间和前额继续敲打这堵特殊的墙壁了。
【问题讨论】:
标签:
authorization
asp.net-identity
identityserver4
【解决方案1】:
不准确。这是为我解决问题的方法。
在 AppSettings.json 中添加名称和角色的范围
"IdentityServer": {
"Clients": {
"PrymeServer": {
"Profile": "IdentityServerSPA",
"Scope": "openid profile name role PrymeServerAPI"
}
}
}
然后在客户 ProfileService 中,将声明添加到上下文中
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var roleClaims = context.Subject.FindAll(JwtClaimTypes.Role);
var nameClaims = context.Subject.FindAll(JwtClaimTypes.Name);
context.IssuedClaims.AddRange(roleClaims);
context.IssuedClaims.AddRange(nameClaims);
return Task.CompletedTask;
}
向 Brock Allen 和 Dominick Baier 大声疾呼,寻求帮助!