JWT web token encryption - SecurityAlgoritms.HmacSha256 vs SecurityAlgoritms.HmacSha256Signature

Ewa*_*ger 5 c# encryption cryptography jwt json-web-token

For token based authentication Microsoft.IdentityModel.Tokens provides a list of security algorithms that can be used to create 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);
Run Code Online (Sandbox Code Playgroud)

What is the difference between HmacSha256 and HmacSha256Signature? When would you use the signature one instead of the non-signature one?**

There are other algorithms "non signature" and "signature" algorithms as well - RsaSha256 and RsaSha256

Mar*_*ham 6

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

的最新定义System.IdentityModel.Tokens.SecurityAlgorithms不包括 HmacSha256,而是允许您将SigningCredentials.

您应该使用看起来已弃用HmacSha256Signature的应用程序来验证您的应用程序的未来HmacSha256

从微软文档...

具有 Signature 后缀的成员可用于指定signatureAlgorithm 参数,具有Digest 后缀的成员可用于指定digestAlgorithm 参数。

  • 当使用 `HmacSha256Signature` 而不是 `HmacSha256` 时 https://jwt.io/ 由于某种原因无法验证签名。 (7认同)
  • @Konrad您仍然需要使用“HmacSha256”来验证算法类型,因为这实际上是 JWT 中的内容 (3认同)