2016-11-01 9 views
1

自分自身で提供する非対称キーを使用して.Net 4.5でJWTトークンを生成したいのですが、System.IdentityModel.Tokens.Jwt、version 4.0でいくつかの問題が発生しています.3。RsaSecurityKeyは引数としてRSAParametersを使用しません

私は2048キーを作成することをお勧めします。 RSA.Create()コンストラクタは1024個のキーを作成します。次のコード切り取り領域がそのhttps://stackoverflow.com/a/38233644注由来

using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(2048)) 
     { 
      var publicPrivate = provider.ToXmlString(true); 
      var publicKeyOnly = provider.ToXmlString(false); 

      var stuff = provider.ExportParameters(true); 

      signingCredentials = new SigningCredentials(new RsaSecurityKey(RSA.Create()), SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest); //no idea how to pull the key out of here. 

     } 

多くの例では、1つはRsaSecurityKeyコンストラクタにRSAParametersをドロップすることができ、今では、(任意の文字列パラメータを用いて)RSA.Create()コンストラクタをとり、この例ではRSAParametersは、私のバージョンではできないRsaSecurityKeyコンストラクタにうまく行きます。私はRSA.Createを使用することに制限されています。

// NOTE: Replace this with your actual RSA public/private keypair! 
var provider = new RSACryptoServiceProvider(2048); 
var parameters = provider.ExportParameters(true); 

// Build the credentials used to sign the JWT 
var signingKey = new RsaSecurityKey(parameters); //not an option for me, unfortunately 
+0

この要素の1つは、4.0.3ではRsaSecurityKeyのパラメータを使用しないことです。プロバイダを使用します。 varプロバイダ=新しいRSACryptoService(2048); var signingKey =新しいRsaSecurityKey(プロバイダ)。 次のリンクにあります。 https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/477 – user7101139

答えて

1

ここに私がしたことがあります。最初に私はデバッガを実行しました。新しいRSACryptoServiceProvider(2048)のToXmlString(Boolean)メソッドを使用して、新しいプロバイダからXMLを取得しました。それから私はそれをストレージ用のXMLファイルにしました。

私はRSAPrametersを持っているので、私は自分自身で提供する鍵を持っています。これは安全なストレージから来る可能性があります - doesnこの答えのための問題ではない。

XmlDocument publicXmlParam = new XmlDocument(); 
publicXmlParam.Load("C:\\rsapublicprivate.xml"); 

// Here I "utilize my own 2048 keys" 
RSACryptoServiceProvider provider = new RSACryptoServiceProvider(2048); 

//This was the trick, we pass the RSA parameters as XML into the provider.   
provider.FromXmlString(publicXmlParam.OuterXml); 

// Then we use the provider in the constructor of the RsaSecurityKey 
var key = new RsaSecurityKey(provider); 

signingCredentials = 
    new SigningCredentials(
     key, 
     SecurityAlgorithms.RsaSha256Signature, 
     SecurityAlgorithms.Sha256Digest); 

私はJWTトークンに署名するために必要な署名資格があります。

関連する問題