具有AspNetIdentity的IdentityServer4-添加角色声明

问题描述 投票:0回答:1

我正在将IdentityServerSPA配置文件与IdentityServer4和AspNetIdentity一起使用。我想将AspNetIdentity中的角色作为角色声明包括在内。我可以在我的自定义ProfileService中看到,角色声明是由AspNetIdentity添加到ClaimPrincipal的,但它们并未将其声明给客户端应用程序。我认为这是“ AllowedScopes”的问题,但我尝试将这些添加到appsettings.json的客户端配置中,但似乎无济于事。我确定我缺少了一些东西,但我的时间和额头用不完,无法继续撞在这堵特殊的墙上。

authorization asp.net-identity identityserver4
1个回答
0
投票

不完全是。这是为我解决问题的方法。

在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;
}

大声呼唤布鲁克·艾伦和多米尼克·拜尔寻求帮助!

© www.soinside.com 2019 - 2024. All rights reserved.