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. その証明書の公開鍵をエクスポートし、検証マシンの信頼できる人々の証明書フォルダーに入れました。
  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 => Key Configuration => Signing Thumbprint
  • thinktecture identityserver 2.Configuration => Relying Party => レルム/スコープ名
  • thinktecture identityserver 2.構成 => 一般構成 => サイト ID

この場合、私が間違っていることがわかりますか?

更新 2

次のコードに出くわしました: http://pastebin.com/DvQz8vdbで JWT を実行した後、同じエラーが表示されました: 基本的に、「RS256」、「HS384」、または「HS512」のみをサポートしていると言っています。おそらくこれは私の問題です..私のJWTは、RS256またはHS > 256(384/512)ではなく、HS256に戻ってきます

署名アルゴリズムを HS256 から HS512 に変更するにはどうすればよいですか?

この時点で、Identity Server の問題に戻ったと思いますか?

4

3 に答える 3

2

偶然この古い投稿に出くわしましたが、ほぼ 1 年前に同様の問題があったため、当時の調査結果について言及します。基本的に、IdSrv V2 に署名証明書の使用を「強制」する方法は、証明書利用者に対して対称署名キーが定義されていないことを確認することです。定義されている限り、常に対称署名キーが使用されます。詳細については、私のブログ記事を参照してください。

これが他の人がここにたどり着くのに役立つことを願っています:-)

于 2014-09-18T14:26:24.330 に答える
1

これでやっと締めくくることができます。署名証明書は、実際には IdentityServer の下の oAuth2 プロトコルの jwt とは何の関係もないようです。どの証明書を使用しても、エラーが発生しました。

IdentityServer の [キー構成] セクションにある署名証明書ではなく、対称署名キーを使用して jwt を検証することで問題を解決しました。

于 2013-08-07T01:12:23.780 に答える
0

これは古い質問であることは知っていますが、まったく同じ問題に遭遇しましたが、問題の原因である CrptoHelper.GetIdentityFromConfig 内の競合状態に関連して接続の問題が見つかりました

于 2015-12-15T14:13:16.693 に答える