【问题标题】:JWT web token encryption - SecurityAlgoritms.HmacSha256 vs SecurityAlgoritms.HmacSha256SignatureJWT Web 令牌加密 - SecurityAlgoritms.HmacSha256 与 SecurityAlgoritms.HmacSha256Signature
【发布时间】:2017-06-11 17:08:28
【问题描述】:

对于基于令牌的身份验证,Microsoft.IdentityModel.Tokens 提供了可用于创建 SigningCredentials 的安全算法列表:

  string secretKey = "MySuperSecretKey";
  byte[] keybytes = Encoding.ASCII.GetBytes(secretKey);
  SecurityKey securityKey = new SymmetricSecurityKey(keybytes);
  SigningCredentials signingCredentials =
                    new SigningCredentials(securityKey,
                        SecurityAlgorithms.HmacSha256);

  SigningCredentials signingCredentials =
                    new SigningCredentials(securityKey,
                        SecurityAlgorithms.HmacSha256Signature);

HmacSha256HmacSha256Signature 有什么区别?您什么时候会使用签名一号而不是非签名一号?**

还有其他“非签名”和“签名”算法。例如,RsaSha256 和 RsaSha256Signature

【问题讨论】:

    标签: c# encryption cryptography jwt


    【解决方案1】:

    HmacSha256 是一个字符串常量,计算结果为“HS256”。 HmacSha256Signature 也是一个字符串常量,但计算结果为“http://www.w3.org/2001/04/xmldsig-more#hmac-sha256

    System.IdentityModel.Tokens.SecurityAlgorithms 的最新定义不包括 HmacSha256,而是允许您将 SigningCredentials 的签名和摘要算法分开。

    您应该使用HmacSha256Signature 来确保您的应用程序不会过时,因为HmacSha256 看起来已被弃用。

    来自 Microsoft 文档...

    具有签名后缀的成员可用于指定 signatureAlgoritm 参数和具有 Digest 后缀的成员 可用于指定 digestAlgorithm 参数。

    【讨论】:

    • 当使用HmacSha256Signature而不是HmacSha256时,jwt.io由于某种原因无法验证签名。
    • 另外,JWT tools.ietf.org/html/rfc7519 的 RFC 没有提到 http 表示法。
    • @Konrad 您仍然需要使用HmacSha256 来验证算法类型,因为这实际上是 JWT 中的内容
    猜你喜欢
    • 2019-07-25
    • 2020-07-15
    • 1970-01-01
    • 2018-11-25
    • 2015-01-02
    • 2016-08-01
    • 2013-08-15
    • 2019-12-08
    • 2019-05-01
    相关资源
    最近更新 更多