【发布时间】:2015-11-18 13:13:21
【问题描述】:
几年来我一直在签署 JWT 令牌,没有任何问题。我最近升级到 Windows 10,当我尝试签署 JWT 令牌时,我收到“指定的算法无效”的 CryptographicException。
我将 X509Certificate2 传递给此方法,但实际上我没有做任何其他事情。
System.IdentityModel.Tokens.JwtSecurityTokenHandler.CreateToken()
签名证书之前是使用 SHA-1 创建的,所以我什至使用 SHA-256 RSA 创建了一个新的签名证书,但仍然没有乐趣。
我阅读了几个 SO 问题,这些问题表明我需要启用“Microsoft Enhanced RSA and AES Cryptographic Provider”或至少使用它创建我的签名证书。很多 SO Qu 建议添加这行,但仍然没有运气。
CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
我还检查了该算法是否在注册表中,并在 Provider 列表和 Provider 类型列表中找到了它。不幸的是,我无法控制 Jwt 签名代码使用的算法,因为它全部隐藏在 System.Identity 库中。
奇怪的是,当我进入代码并查看签名时,它提到它有一个“Microsoft 强加密提供程序”的 RSA 提供程序,而不是前面提到的“增强”提供程序,尽管从 MSDN 文档中它们似乎是差不多。
我认为它抱怨找不到用于签署实际令牌的算法,而不是用于读取或解释签名证书的算法?
利用 JwtSecurityTokenHandler 代码的有限灵活性,我还能做哪些其他检查?
完整的堆栈跟踪是:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash)
at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash)
at System.IdentityModel.Tokens.AsymmetricSignatureProvider.Sign(Byte[] input)
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.CreateSignature(String inputString, SecurityKey key, String algorithm, SignatureProvider signatureProvider)
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.CreateToken(String issuer, String audience, ClaimsIdentity subject, Lifetime lifetime, SigningCredentials signingCredentials, SignatureProvider signatureProvider)
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.CreateToken(SecurityTokenDescriptor tokenDescriptor)
【问题讨论】:
-
不是重复的,但似乎与以下问题相同:stackoverflow.com/questions/7444586/…
标签: cryptography rsa jwt sha256 signing