执行 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 上的证书有关。
- 我为 idP 创建了一个 sha256 签名证书,并将其放入 idP 的个人证书中。
- 我导出了该证书的公钥并放入了我的验证机的受信任人员的 Cert 文件夹中。
- 从我的 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?
在这一点上,我想我们又回到了身份服务器问题?