【问题标题】:Missing Claims via JWT Web Token通过 JWT Web 令牌丢失声明
【发布时间】:2020-06-01 20:54:15
【问题描述】:

我有以下代码在 webapi 核心应用程序中创建 JWT 令牌。

var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
                var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

                var claims = new[] {
                    new Claim(JwtRegisteredClaimNames.Sub, userInfo.Username!=null?userInfo.Username:string.Empty),
                    new Claim(JwtRegisteredClaimNames.Email, userInfo.Email!=null?userInfo.Email:string.Empty),
                    new Claim(JwtRegisteredClaimNames.Jti, userInfo.AccountId!=null?userInfo.AccountId.ToString():Guid.Empty.ToString()),
                    new Claim(JwtRegisteredClaimNames.NameId, userInfo.UserId!=null?userInfo.UserId.ToString():Guid.Empty.ToString()),
                    new Claim(JwtRegisteredClaimNames.UniqueName, userInfo.ClientId!=null?userInfo.ClientId.ToString():Guid.Empty.ToString())
                };

                var token = new JwtSecurityToken(_config["Jwt:Issuer"],
                  _config["Jwt:Issuer"],
                  claims,
                  null,
                  expires: DateTime.Now.AddMinutes(20),
                  signingCredentials: credentials);

                return new JwtSecurityTokenHandler().WriteToken(token);

但是,当我在客户端调用 webapi 端点后尝试取回声明时,我只有以下声明。如您所见,“JTI”声明很好,但“UniqueName”声明丢失了。

[0]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: }
    [1]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: }
    [2]: {jti: f7c5af77-d0c4-4026-9b33-2fe9fbf5ee28}
    [3]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: 00000000-0000-0000-0000-000000000000}
    [4]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: b34a1b42-df24-42a3-93d7-68e88523602f}
    [5]: {exp: 1581973428}
    [6]: {iss: Test.com}
    [7]: {aud: Test.com}

我需要做什么才能让声明出现?

请注意,我设置为 JwtRegisteredClaimNames.UniqueName 的声明确实出现在声明中,但不在该名称下。它的值是在item下设置的

[4]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: b34a1b42-df24-42a3-93d7-68e88523602f}

但是,当使用以下内容时,它不会被带回来,因为它是空的

identity.FindFirst(JwtRegisteredClaimNames.UniqueName).Value;

【问题讨论】:

    标签: jwt asp.net-core-webapi


    【解决方案1】:

    但是,当使用以下内容时,它不会被带回来,因为它是空的

    identity.FindFirst(JwtRegisteredClaimNames.UniqueName).Value;

    要解决上述问题,您可以尝试使用 Startup.cs 中的以下代码 sn-p 清除入站声明类型映射

    System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
    

    测试结果

    JwtRegisteredClaimNames.UniqueName设置测试值

    //just for testing purpose
    
    new Claim(JwtRegisteredClaimNames.UniqueName, "b34a1b42-df24-42a3-93d7-68e88523602f")
    

    可以使用identity.FindFirst(JwtRegisteredClaimNames.UniqueName).Value得到期望值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-23
      • 2021-04-30
      • 1970-01-01
      • 2021-09-25
      • 2020-11-19
      • 2021-02-13
      • 2019-06-03
      • 2019-03-24
      相关资源
      最近更新 更多