【问题标题】:how to add role to userinfo endpoint in identity server如何将角色添加到身份服务器中的 userinfo 端点
【发布时间】:2017-12-15 18:26:40
【问题描述】:

我一直在使用身份服务器快速启动应用程序,我想在调用userinfo端点时添加要返回的角色信息。

  public Claim[] GetUserClaims(UserServiceProxy.Dto.User user)

    {
        return new Claim[]
        {
            new Claim(JwtClaimTypes.Name, user.Username), 
            new Claim(JwtClaimTypes.Email, user.Email),
            new Claim(JwtClaimTypes.GivenName, user.Firstname),
            new Claim(JwtClaimTypes.FamilyName, user.Lastname),
            new Claim(JwtClaimTypes.Subject, user.Username),
            new Claim(JwtClaimTypes.Role, user.Role ?? "normal"), //have added this
        };
    }

这是从我的 GetProfileDataAsync 调用的

if (context.RequestedClaimTypes.Any())
        {
            var user = UserClient.FindByUserName(context.Subject.GetSubjectId());
            if (user != null)
            {
                context.AddRequestedClaims(UserClient.GetUserClaims(user.Result.User));
            }
        }

当我调用 /connect/userinfo 端点时,我得到了这个(无角色):

{"name":"joe3","given_name":"Joe","family_name":"Three","sub":"joe3"}

【问题讨论】:

    标签: asp.net identityserver4


    【解决方案1】:

    没关系,我发现问题在于我需要:

    1. 将角色的新身份资源添加到返回的列表中 获取身份资源

      public static IEnumerable<IdentityResource> GetIdentityResources()
      {
          return new List<IdentityResource>
          {
              new IdentityResources.OpenId(),
              new IdentityResources.Profile(),
              new IdentityResources.Email(),
              new IdentityResource{Name = "roles", UserClaims={JwtClaimTypes.Role}}
          };
      }
      
    2. 将角色范围添加到客户端的 AllowedScopes 列表中

      AllowedScopes = new List<string>
                  {
                      IdentityServerConstants.StandardScopes.OpenId,
                      IdentityServerConstants.StandardScopes.Profile,
                      IdentityServerConstants.StandardScopes.Email,
                      "roles",
      
                  },
      
    3. 在请求中添加角色范围的请求。

            "RequestedScopes": "openid profile email roles",
      

    【讨论】:

    • 是的,关键是在您的ConfigureServices 中,除了ApiResources 之外,您还以某种方式添加了IdentityResources。您随后定义的 IdentityResources 将在 userInfo 端点上提供
    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2020-01-13
    • 2016-05-17
    • 2023-03-03
    • 2015-04-06
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多