【问题标题】:IDX10632: SymmetricSecurityKey.GetKeyedHashAlgorithm( 'HS512' ) threw an exception when validating JWT using HS512 ALGOIDX10632:使用 HS512 ALGO 验证 JWT 时,SymmetricSecurityKey.GetKeyedHashAlgorithm('HS512') 引发异常
【发布时间】:2020-12-23 21:28:04
【问题描述】:

我正在使用 NuGet 包 System.IdentityModel.Tokens.Jwt 版本 4.0.4.403061554。

我有一个验证 JWT 的实现,它适用于算法 HS256。

但是,如果我将我的 JWT 更改为使用算法 HS512 生成,那么我会在验证期间收到错误消息。

System.IdentityModel.SignatureVerificationFailedException
  HResult=0x80131501
  Message=IDX10503: Signature validation failed. Keys tried: 'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey
'.
Exceptions caught:
 'System.InvalidOperationException: IDX10632: SymmetricSecurityKey.GetKeyedHashAlgorithm( 'HS512' ) threw an exception.
SymmetricSecurityKey: 'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey'
SignatureAlgorithm: 'HS512', check to make sure the SignatureAlgorithm is supported.

我尝试过生成 512 位密钥,我也尝试过较小的密钥,例如 256 位密钥(适用于算法 HS256 的密钥),但没有任何效果。

我的实现是这样的:

                    InMemorySymmetricSecurityKey signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes("secretsigningkey"));
                    TokenValidationParameters tokenValidationParameters = new TokenValidationParameters()
                    {
                        ValidAudiences = validAudiences,
                        ValidIssuers = validIssuers,
                        IssuerSigningKey = signingKey
                    };
                    JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
                    var claimsPrincipal = tokenHandler.ValidateToken(token, tokenValidationParameters, out SecurityToken validatedToken);

这个方法tokenHandler.ValidateToken抛出异常。

如何更改我的代码以允许 HS512(以及任何其他类型的 JWT 支持的算法)??

【问题讨论】:

    标签: c# security encoding jwt identity


    【解决方案1】:
    1. 根据此MSDN 文档,SecurityAlgorithms 不支持验证 HS512 算法。
    1. 安装 jwt 解码/验证库:JWT 及其项目站点:https://github.com/jwt-dotnet/jwt
    PM> Install-Package JWT -Version 7.2.1
    
    1. 使用JWT官方文档,使用HS512算法修改你的示例代码,会是这样的:
    try
    {
        IJsonSerializer serializer = new JsonNetSerializer();
        var provider = new UtcDateTimeProvider();
        IJwtValidator validator = new JwtValidator(serializer, provider);
        IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
        IJwtAlgorithm algorithm = new HMACSHA512Algorithm();
        IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder, algorithm);
    
        json = decoder.Decode(token, "secretsigningkey", verify: true);
        Console.WriteLine(json);
    }
    catch (TokenExpiredException)
    {
        Console.WriteLine("Token has expired");
    }
    catch (SignatureVerificationException)
    {
        Console.WriteLine("Token has invalid signature");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Other exception: " + ex.Message);
    }
    

    【讨论】:

    • 感谢您的努力。您的解决方案确实适用于 HMACSHA512,但是我真的试图在可能的情况下使用 System.IdentityModel.Tokens.Jwt 包来实现,同时也是一种灵活使用算法的方法。
    【解决方案2】:

    我将包 System.IdentityModel.Tokens.Jwt 的版本升级到 6.7.1(可能没有必要)。

    真正的解决办法是改变这一点:

    InMemorySymmetricSecurityKey signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes("secretsigningkey"));
    

    到这里

    SecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(domainJWTParams.IssuerSigningKey));
    

    这适用于 HS256、HS384、HS512(可能甚至更多),但这种实现的真正好处是不需要硬编码算法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      相关资源
      最近更新 更多