【问题标题】:How to add roles to claims in IdentityServer4?如何在 IdentityServer4 中为声明添加角色?
【发布时间】:2019-05-30 15:24:01
【问题描述】:

我是 IdentityServer 的新手,我整天都在为这个问题苦苦挣扎。以至于我几乎要放弃这个了。我知道这个问题已经被问了一遍又一遍,我尝试了许多不同的解决方案,但似乎都没有奏效。希望你能帮助我把我推向正确的方向。

首先我通过运行dotnet new -i identityserver4.templates 安装了IdentityServer4 模板,并通过运行dotnet new is4aspid -o IdentityServer 创建了一个带有is4aspid 模板的新项目。

之后,我创建了一个新的 IdentityServer 数据库并运行了迁移。到那时我已经有了一个默认的身份数据库结构。

在 Config.cs 中,我将 MVC client 更改为以下内容:

new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",

    AllowedGrantTypes = GrantTypes.Implicit,
    ClientSecrets = { new Secret("47C2A9E1-6A76-3A19-F3C0-S37763QB36D9".Sha256()) },

    RedirectUris = { "https://localhost:44307/signin-oidc" },
    FrontChannelLogoutUri = "https://localhost:44307/signout-oidc",
    PostLogoutRedirectUris = { "https://localhost:44307/signout-callback-oidc" },

    AllowOfflineAccess = true,
    AllowedScopes = { "openid", "profile", "api1", JwtClaimTypes.Role }                
},

并将GetApis 方法更改为:

public static IEnumerable<ApiResource> GetApis()
{
    return new ApiResource[]
    {
        new ApiResource("api1", "My API #1", new List<string>() { "role" })
    };
}

当然,数据库中还没有用户,所以我添加了一个注册表单并注册了两个虚拟用户,一个是用户名admin@example.com,另一个是用户名subscriber@example.com

为了将角色分配给这些用户,我在 Startup.cs 中创建了以下方法。

private async Task CreateUserRoles(IServiceProvider serviceProvider) {
    var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
    var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

    IdentityResult adminRoleResult;
    IdentityResult subscriberRoleResult;

    bool adminRoleExists = await RoleManager.RoleExistsAsync("Admin");
    bool subscriberRoleExists = await RoleManager.RoleExistsAsync("Subscriber");

    if (!adminRoleExists) {
        adminRoleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
    }

    if(!subscriberRoleExists) {
        subscriberRoleResult = await RoleManager.CreateAsync(new IdentityRole("Subscriber"));
    }

    ApplicationUser userToMakeAdmin = await UserManager.FindByNameAsync("admin@example.com");
    await UserManager.AddToRoleAsync(userToMakeAdmin, "Admin");

    ApplicationUser userToMakeSubscriber = await UserManager.FindByNameAsync("subscriber@example.com");
    await UserManager.AddToRoleAsync(userToMakeSubscriber, "Subscriber");
}

在同一个类的Configure方法中,我添加了参数IServiceProvider services,并像这样调用上面的方法:CreateUserRoles(services).Wait();。此时我的数据库中确实有两个角色。

接下来,我创建了一个新的解决方案(在同一个项目中),并在该解决方案的 Startup.cs 文件中,我在 ConfigureServices 方法中添加了以下内容。

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Cookies";
        options.DefaultChallengeScheme = "oidc";
    })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options => {
            options.SaveTokens = true;
            options.ClientId = "mvc";
            options.ClientSecret = "32D7A7W0-0ALN-2Q44-A1H4-A37990NN83BP";
            options.RequireHttpsMetadata = false;
            options.Authority = "http://localhost:5000/";
            options.ClaimActions.MapJsonKey("role", "role");
        });

之后我在同一个类的Configure方法中添加了app.UseAuthentication();

然后我使用以下 if 语句创建了一个新页面。

if(User.Identity.IsAuthenticated) {
 <div>Yes, user is authenticated</div>
} 

if(User.IsInRole("ADMIN")) {
 <div>Yes, user is admin</div>
}

我使用 admin@example.com 登录,但第二个 if 语句返回 False。我通过像这样遍历它们来检查所有声明。

@foreach (var claim in User.Claims) {
    <dt>@claim.Type</dt>
    <dd>@claim.Value</dd>
}

但是没有找到角色声明,只有 sid、sub、idp、preferred_username 和 name。

我试图在那里获得角色,以便第二个 if 语句返回 True,但在尝试和尝试之后,我还没有能够使其工作。有人可以看到我必须做什么才能完成这项工作吗?我是 IdentityServer4 的绝对初学者,并尽我所能理解它。任何帮助将不胜感激。提前致谢!

编辑 1:

感谢this questionthis question,我感觉自己走在了正确的轨道上。我做了一些修改,但我仍然无法让它工作。我刚刚尝试了以下方法。

  1. 在我的 IdentityServer 项目中创建了一个新的 ProfileService 类,其内容如下。
public class MyProfileService : IProfileService {
 public MyProfileService() { }
 public Task GetProfileDataAsync(ProfileDataRequestContext context) {
  var roleClaims = context.Subject.FindAll(JwtClaimTypes.Role);
  List<string> list = context.RequestedClaimTypes.ToList();
  context.IssuedClaims.AddRange(roleClaims);
  return Task.CompletedTask;
 }

 public Task IsActiveAsync(IsActiveContext context) {
  return Task.CompletedTask;
 }
}

接下来我通过添加services.AddTransient&lt;IProfileService, MyProfileService&gt;(); 行在ConfigureServices 方法中注册了这个类。之后,我在 GetIdentityResources 方法中添加了一个新行,现在看起来像这样。

public static IEnumerable<IdentityResource> GetIdentityResources()
{
    return new IdentityResource[]
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Profile(),
        new IdentityResource("roles", new[] { "role" })
};
}

我还将角色添加到我的 Mvc 客户端,如下所示:AllowedScopes = { "openid", "profile", "api1", "roles" }

接下来我切换到另一个项目并在 .AddOpenIdConnect oidc 中添加了以下几行。

options.ClaimActions.MapJsonKey("role", "role", "role");
options.TokenValidationParameters.RoleClaimType = "role";

但是,我仍然无法让它像我想要的那样工作。有人知道我错过了什么吗?

【问题讨论】:

  • 搜索这个确切的问题,我已经在这里回答了,只是现在无法在移动设备上查看。
  • 感谢您的帮助,我确实看到了您的回答并尝试实施您的解决方案。但是,第二个 if 语句仍然返回 False,并且该角色不是声明的一部分。我已经用我添加的代码编辑了我的帖子。如果您能向我解释我做错了什么,那将非常有帮助。提前致谢!
  • 到目前为止看起来不错,我会大胆猜测并说它可能区分大小写,所以试试IsInRole("Admin")。还可以使用 jwt.io 之类的东西检查原始令牌,并检查 role 声明现在是否已正确添加到令牌中。此外,您还没有按照我的回答实现您的ProfileService
  • @Mitch,在您的客户端配置中看不到options.ResponseType = "id_token token";,因此您只请求IdentityResiource 信息而不是ApiResource。尽管如此,在您编辑之后,您应该能够使用 id_token 获得您的角色,但您必须明确提出要求:options.Scope.Add("roles");
  • 不幸的是,他们都不是这样。无论我将 isInRole("ADMIN") 更改为 isInRole("Admin") 还是更改为 isInRole("admin"),它们都返回 False。解码的 JWT 令牌也不包含角色密钥。但是,如果我添加 options.Scope.Add("roles");到客户端它返回角色属性并且 isInRole("Admin") 返回真!感谢您的建议和帮助!它现在应该像现在一样工作:)

标签: c# .net-core identityserver4


【解决方案1】:

您需要做两件事来确保您在声明中获得用户角色:

1- 在 IdentityServer4 项目中:您需要实现 IProfileService http://docs.identityserver.io/en/latest/reference/profileservice.html

不要忘记像这样在startup.cs文件中添加类

services.AddIdentityServer()
// I just removed some other configurations for clarity
                **.AddProfileService<IdentityProfileService>();**

2-在Web Client项目的startup.cs文件中:配置openId的时候,必须要提一下:

services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.SignInScheme = "Cookies";
            options.Authority = "Identity URL ";
            options.RequireHttpsMetadata = true;

            options.ClientId = "saas_crm_webclient";
            options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
            options.ResponseType = "code id_token";
            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = false;

            options.Scope.Add("test.api");
            options.Scope.Add("identity.api");
            options.Scope.Add("offline_access");


            **options.ClaimActions.Add(new JsonKeyClaimAction("role", null, "role"));**

            **options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            };**
        });

【讨论】:

    【解决方案2】:

    Slightly different question, absolutely matching answer.

    使用 Edit 1,IdP 配置看起来足以在 在请求时 提供具有角色的 identityaccess 令牌>。剩下的就是配置客户端请求访问令牌(.Net 客户端默认不这样做),或者只是请求身份令牌内的roles 范围。

    要获得id_token 的角色,客户端配置必须包含options.Scope.Add("roles");

    要使用不记名令牌获取角色,必须通过在客户端配置中指定 options.ResponseType = "id_token token"; 来请求该令牌。

    【讨论】:

      【解决方案3】:

      我在 .NET 5 中是这样做的:

      Startup.cs 中的services.AddAuthentication 之前添加JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

      https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/1349

      我也加了

      services.AddScoped<IProfileService, ProfileService>();
      

      ProfileService.cs 看起来像这样将角色映射到声明:

      public sealed class ProfileService : IProfileService
      {
          private readonly IUserClaimsPrincipalFactory<ApplicationUser> _userClaimsPrincipalFactory;
          private readonly UserManager<ApplicationUser> _userMgr;
          private readonly RoleManager<IdentityRole> _roleMgr;
      
          public ProfileService(
              UserManager<ApplicationUser> userMgr,
              RoleManager<IdentityRole> roleMgr,
              IUserClaimsPrincipalFactory<ApplicationUser> userClaimsPrincipalFactory)
          {
              _userMgr = userMgr;
              _roleMgr = roleMgr;
              _userClaimsPrincipalFactory = userClaimsPrincipalFactory;
          }
      
          public async Task GetProfileDataAsync(ProfileDataRequestContext context)
          {
              string sub = context.Subject.GetSubjectId();
              ApplicationUser user = await _userMgr.FindByIdAsync(sub);
              ClaimsPrincipal userClaims = await _userClaimsPrincipalFactory.CreateAsync(user);
      
              List<Claim> claims = userClaims.Claims.ToList();
              claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();
      
              if (_userMgr.SupportsUserRole)
              {
                  IList<string> roles = await _userMgr.GetRolesAsync(user);
                  foreach (var roleName in roles)
                  {
                      claims.Add(new Claim(JwtClaimTypes.Role, roleName));
                      if (_roleMgr.SupportsRoleClaims)
                      {
                          IdentityRole role = await _roleMgr.FindByNameAsync(roleName);
                          if (role != null)
                          {
                              claims.AddRange(await _roleMgr.GetClaimsAsync(role));
                          }
                      }
                  }
              }
      
              context.IssuedClaims = claims;
          }
      
          public async Task IsActiveAsync(IsActiveContext context)
          {
              string sub = context.Subject.GetSubjectId();
              ApplicationUser user = await _userMgr.FindByIdAsync(sub);
              context.IsActive = user != null;
          }
      }
      

      来源:

      https://ffimnsr.medium.com/adding-identity-roles-to-identity-server-4-in-net-core-3-1-d42b64ff6675

      【讨论】:

        猜你喜欢
        • 2020-08-20
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 2020-12-30
        • 2017-07-26
        • 2021-04-10
        • 2020-03-27
        相关资源
        最近更新 更多