【问题标题】:Roles not in Token via IdentityServer4通过 IdentityServer4 不在令牌中的角色
【发布时间】:2017-04-06 16:30:47
【问题描述】:

我正在使用 IdentityServer4 来验证我的内部应用程序。我在我的 GrantValidator 内部调用了一项服务来验证用户名和密码。该服务返回用户的角色列表

    public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
    {


        var loginResponse = await loginService.ValidateCreds(context.UserName, context.Password)

        if (loginResponse.Success)
        {
            var roleClaims = loginResponse.Roles?.Select(x => new Claim(ClaimTypes.Role, x)) ?? Enumerable.Empty<Claim>();
            // The claims argument doesn't seem to do anything!
            context.Result = new GrantValidationResult(loginResponse.UserId, "password", roleClaims);
        }
        else
            context.Result = new GrantValidationResult(TokenRequestErrors.InvalidClient, "Invalid Credentials");
    }

返回的令牌:

{
  "nbf": 1491409664,
  "exp": 1491413264,
  "iss": "http://localhost:5000",
  "aud": [
    "http://localhost:5000/resources",
    "redacted"
  ],
  "client_id": "localhost",
  "sub": "nouser_222222222|1234567",
  "auth_time": 1491409634,
  "idp": "local",
  "clientid": "dfc962d7-c731-4d42-b0c8-bec766dc7813",
  "scope": [
    "profile",
    "redacted"
  ],
  "amr": [
    "password"
  ]
}

【问题讨论】:

    标签: openid-connect identityserver4


    【解决方案1】:

    配置文件服务负责将声明放入令牌中。实现IProfileService并在DI中注册。

    在那里,您可以访问资源所有者验证器放入主题的声明,然后可以通过将它们放入 IssuedClaims 集合来发出它们。

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 2019-09-13
      • 2021-10-04
      • 1970-01-01
      • 2019-03-23
      • 2018-07-07
      相关资源
      最近更新 更多