【问题标题】:Custom Claims in Access Token访问令牌中的自定义声明
【发布时间】:2016-05-26 06:59:23
【问题描述】:

1-) 我正在尝试向访问令牌添加一些声明,在调试时我观察到,它已添加到 AuthenticationResult-Claims,但我在 JWT 访问令牌中看不到。请在下面找到图片以供参考。我添加了一个名为“mem”值 123 的声明。

我在 AuthenticationLocalAsync 方法中添加了如下内容。

IEnumerable claim = new Claim[] { new Claim("mem", "123") };

        context.AuthenticateResult =
            new AuthenticateResult(context.UserName, context.UserName, claim);

debugging, It includes the custom claim

2-) 第二个问题是 - 当我使用 PostMan 作为客户端时,GetProfileDataAsync 方法没有被执行,因为 API 服务器正在为移动客户端开发,所以我使用的是 PostMan 客户端。在登录本身中,我想获得我在第一点中提到的声明。请帮助,我已经花了很多时间弄清楚这两点。

谢谢

【问题讨论】:

  • 您是否将此声明“mem”定义为任何范围内的声明?
  • 没有。我没有在任何范围内定义此声明。
  • 定义一个新的范围并在该范围下声明。请求令牌时,请求此范围。

标签: openid-connect identityserver3


【解决方案1】:
  1. 为您的声明指定资源范围或将您的声明添加到现有资源范围。

https://identityserver.github.io/Documentation/docsv2/configuration/scopesAndClaims.html

var scope = new Scope
{
    Name = "memScope",
    DisplayName = "Scope for mem claim",
    Type = ScopeType.Resource,
    Claims = new List<ScopeClaim>
    {
        new ScopeClaim("mem")
    }
};
  1. 确保您的客户端可以请求该范围(通过将 AllowAccessToAllScopes 设置为 true 或在客户端上明确指定 AllowedScopes)。

https://identityserver.github.io/Documentation/docsv2/configuration/clients.html

  1. 在您的客户端中请求范围。

【讨论】:

    【解决方案2】:

    首先我想提一下我还不是身份服务器专家。但我会尽力而为。

    我个人不会在 AuthenticateLocalAsync 方法中分配声明。此逻辑应移至声明提供程序。

    但是,如果您坚持在身份验证步骤中添加声明,您的问题仍然很可能出在 ClaimsProvider 上。令牌中包含的声明由 ClaimsProvider 直接处理。因此,GetAccessTokenClaimsAsync() 可能甚至不包括添加到主体的声明,或者存在某种明确阻止返回您的自定义声明的逻辑。

    这是我的最佳猜测。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-05
      • 2021-07-25
      • 2020-01-15
      • 2021-12-01
      • 2022-10-07
      • 2017-11-29
      • 2019-11-20
      • 2019-06-06
      相关资源
      最近更新 更多