JwtSecurityTokenHandler().ValidateToken()::签名验证失败...在此上下文中不支持sha256

use*_*146 11 c# x509securitytokenmanager wif jwt

我执行JwtSecurityTokenHandler()时遇到以下错误.ValidateToken()函数:

这是我的伪代码:

var jwtToken = {...}
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters {...};
var claimsPrincipal = tokenHandler.ValidateToken(jwtToken, validationParameters);
Run Code Online (Sandbox Code Playgroud)

这是错误:

Jwt10316: Signature validation failed. Keys tried: 'System.IdentityModel.Tokens.X509AsymmetricSecurityKey'.
Exceptions caught:
 'System.InvalidOperationException: Jwt10518: AsymmetricSecurityKey.GetHashAlgorithmForSignature( 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' ) threw an exception.
AsymmetricSecurityKey: 'System.IdentityModel.Tokens.X509AsymmetricSecurityKey'
SignatureAlgorithm: 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256', check to make sure the SignatureAlgorithm is supported.
Exception: 'System.NotSupportedException: Crypto algorithm 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' not supported in this context.
   at System.IdentityModel.Tokens.X509AsymmetricSecurityKey.GetHashAlgorithmForSignature(String algorithm)
   at System.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(AsymmetricSecurityKey key, String algorithm, Boolean willCreateSignatures)'. 
---> System.NotSupportedException: Crypto algorithm 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256' not supported in this context.
   at System.IdentityModel.Tokens.X509AsymmetricSecurityKey.GetHashAlgorithmForSignature(String algorithm)
   at System.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(AsymmetricSecurityKey key, String algorithm, Boolean willCreateSignatures)
   --- End of inner exception stack trace ---
   at System.IdentityModel.Tokens.AsymmetricSignatureProvider..ctor(AsymmetricSecurityKey key, String algorithm, Boolean willCreateSignatures)
   at System.IdentityModel.Tokens.SignatureProviderFactory.CreateProvider(SecurityKey key, String algorithm, Boolean willCreateSignatures)
   at System.IdentityModel.Tokens.SignatureProviderFactory.CreateForVerifying(SecurityKey key, String algorithm)
   at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(SecurityKey key, String algorithm, Byte[] encodedBytes, Byte[] signature)
   at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(JwtSecurityToken jwt, Byte[] signatureBytes, IEnumerable`1 signingTokens)'.
Run Code Online (Sandbox Code Playgroud)

System.NotSupportedException:加密算法' http://www.w3.org/2001/04/xmldsig-more#hmac-sha256 '

奇怪的部分是错误消息的这一部分之外的是被编码到令牌中的声明.作为解决方法,我正在进行一些文本解析和重构我的ClaimsPrincipal,但我不应该这样做.

有关如何为此上下文启用sha256的任何想法?

更新:由于我没有在这个问题上有任何动作(除了获得风滚草徽章)我将添加更多细节也许有人可以帮助我解决问题的来源.我必须假设,因为没有其他人遇到这个问题,我必须在某处出现用户错误.如果听起来不对劲,请告诉我.

我的猜测是,由于我们失败了jwt验证,那么它可能与验证机器/ idP上的证书有关.

  1. 我为idP创建了sha256签名证书,并将其放入idP的个人证书中.
  2. 我导出了该证书的公钥,并将其放入我的验证机器的受信任人员的Cert文件夹中.
  3. 然后,我从我的idP收到一个令牌后,在我的验证机器上运行以下代码:

例:

var jwtToken = response.AccessToken;
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert = store.Certificates.Find(X509FindType.FindByThumbprint, "thinktecture identityserver 2.Configuration => Key Configuration => Signing Thumbprint>", false)[0];
store.Close();
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters
                {
                    AllowedAudience = "<thinktecture identityserver 2.Configuration => Relying Party => Realm/Scope Name>",
                    ValidIssuer = "<thinktecture identityserver 2.Configuration => General Configuration => Site ID>",
                    SigningToken = new X509SecurityToken(cert)
                };

ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(jwtToken, validationParameters);
Run Code Online (Sandbox Code Playgroud)

请注意我使用以下占位符来显示数据的填充位置:

  • thinktecture identityserver 2.Configuration => Key Configuration =>签名指纹
  • thinktecture identityserver 2.Configuration =>依赖方=>领域/范围名称
  • thinktecture identityserver 2.Configuration =>常规配置=>站点ID

在这种情况下,你有什么可以看到我做错了吗?

更新2

我遇到了这个代码:http://pastebin.com/DvQz8vdb,在运行我的JWT之后,我给了我同样的错误:基本上它说它只支持"RS256","HS384"或"HS512".也许这是我的问题..我的JWT回来了HS256,而不是RS256或HS> 256(384/512)

如何将签名算法从HS256更改为HS512?

在这一点上,我想我们又回到了身份服务器问题?

use*_*146 1

我终于可以结束这一切了。看来签名证书实际上与IdentityServer下的oAuth2协议中的jwt无关。无论我使用什么证书,我都会收到错误。

我通过使用对称签名密钥来验证 jwt(而不是在 IdentityServer 的密钥配置部分下找到的签名证书)解决了该问题。

  • 我正在寻找使用对称签名密钥验证 JWT 的示例。您有一个示例可以分享如何在代码中执行此操作吗? (7认同)