【问题标题】:IDX10503: Signature validation failed. Token does not have a kid. Keys tried: 'System.Text.StringBuilder'IDX10503:签名验证失败。托肯没有孩子。尝试的键:'System.Text.StringBuilder'
【发布时间】:2021-08-12 06:33:53
【问题描述】:

我有以下 JWT 令牌,

eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnRpZCIsImF1ZCI6ImNsaWVudGlkIiwic3ViIjoiMTIzIiwiYSI6IjQ1NiIsImlhdCI6MTYyMTc5OTU5OCwiZXhwIjoxNjIxNzk5NjU4fQ.hglbX63zhPwTOsB-zSiOMfxEKl5OaIk6zX1o9-LEhP3nro8fa5_3QyIH7I5971j-xuO1bccX1TOh0kNcQ-ACAg

这是使用生成的,

    public static string GenerateToken(string key, string a1, string a2)
    {
        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
        var token = new JwtSecurityToken(
            claims: new Claim[]
            {
            new Claim(JwtRegisteredClaimNames.Iss, "clientid"),
            new Claim(JwtRegisteredClaimNames.Aud, "clientid"),
            new Claim(JwtRegisteredClaimNames.Sub, a1),
            new Claim("a", a2),
            new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
            },
            //notBefore: new DateTimeOffset(DateTime.Now).DateTime,
            expires: new DateTimeOffset(DateTime.Now.AddMinutes(1)).DateTime,
            signingCredentials: new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha512)
        );

        return new JwtSecurityTokenHandler().WriteToken(token);
    }

var key = "Ym7AD3OT2kpuIRcVAXCweYhV64B0Oi9ETAO6XRbqB8LDL3tF4bMk9x/59PljcGbP5v38BSzCjD1VTwuO6iWA8uzDVAjw2fMNfcT2/LyRlMOsynblo3envlivtgHnKkZj6HqRrG5ltgwy5NsCQ7WwwYPkldhLTF+wUYAnq28+QnU=";
// Key is test                
var token = GenerateToken(key, "123", "456");

获得令牌后,我正在使用以下代码进行验证,

var key = "Ym7AD3OT2kpuIRcVAXCweYhV64B0Oi9ETAO6XRbqB8LDL3tF4bMk9x/59PljcGbP5v38BSzCjD1VTwuO6iWA8uzDVAjw2fMNfcT2/LyRlMOsynblo3envlivtgHnKkZj6HqRrG5ltgwy5NsCQ7WwwYPkldhLTF+wUYAnq28+QnU=";
// key is test

var hmac = new HMACSHA512(Convert.FromBase64String(key));
var validationParameters = new TokenValidationParameters
            {
                ValidAudience = "clientid",
                ValidIssuer = "clientid",
                IssuerSigningKey = new SymmetricSecurityKey(hmac.Key)
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            return tokenHandler.ValidateToken(token, validationParameters, out var validToken);

但我遇到了错误,

IDX10503: Signature validation failed. Token does not have a kid. Keys tried: 'System.Text.StringBuilder'.
Exceptions caught:
 'System.Text.StringBuilder'.
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.

【问题讨论】:

  • jwt.io 说签名已验证
  • 再试一次。它告诉错误的签名
  • 否,已验证。
  • 我发现了问题。使用 Convert.FromBase64String 而不是 Encoding.UTF8.GetBytes

标签: c# security jwt identitymodel


【解决方案1】:

我收到相同的错误消息,但我的问题不同。问题出在这一行:

new Ed25519PublicKeyParameters(Encoding.UTF8.GetBytes(key), 0);

所以我改成这样:

new Ed25519PublicKeyParameters(Convert.FromBase64String(key), 0);

密钥以 base64 格式保存到文件中,但我将其加载为 UTF8 字节。

【讨论】:

  • 是的,这与我遇到的问题相同,但在您的情况下,情况并非如此,换句话说,它是编码字节问题:)
【解决方案2】:

问题是这一行,

var hmac = new HMACSHA512(Convert.FromBase64String(key));

我改成,

var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(key));

该错误具有误导性。错误的源代码在https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/d6f2b66d788195b50f2b1f700beb497851194c73/src/System.IdentityModel.Tokens.Jwt/JwtSecurityTokenHandler.cs#L1016

【讨论】:

  • 干得好。在string.IsNullOrEmpty(jwtToken.Header.Kid);keysAttempted.Length > 0 发生时任何 都会引发错误,这是误导性的。
猜你喜欢
  • 2016-01-13
  • 2021-08-01
  • 2018-12-20
  • 2018-02-19
  • 2018-01-03
  • 2017-12-31
  • 2020-12-28
  • 1970-01-01
  • 2019-04-24
相关资源
最近更新 更多