13

执行 JwtSecurityTokenHandler().ValidateToken() 函数时出现以下错误:

这是我的伪代码:

var jwtToken = {...}
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters {...};
var claimsPrincipal = tokenHandler.ValidateToken(jwtToken, validationParameters);

这是错误:

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)'.

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);

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

  • thinktecture identityserver 2.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?

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

4

3 回答 3

2

偶然发现这篇旧帖子,但由于我大约一年前遇到了类似的问题,所以我会提到我当时的发现。基本上“强制”IdSrv V2 使用签名证书的方法是确保没有为依赖方定义对称签名密钥。只要定义了它,它将始终使用对称签名密钥。有关详细信息,请参阅我的博客文章

希望这可以帮助其他人在这里结束:-)

于 2014-09-18T14:26:24.330 回答
1

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

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

于 2013-08-07T01:12:23.780 回答
0

我知道这是一个老问题,但我遇到了完全相同的问题,但发现了与 CrptoHelper.GetIdentityFromConfig 内的竞争条件相关的连接问题,这就是导致问题的原因

于 2015-12-15T14:13:16.693 回答