2017-04-12 70 views
0

我正在使用c#.NET創建JWT令牌。我有我自己的私鑰作爲XML。我需要將它作爲函數signingCredentials的參數之一傳遞。我的代碼如下所示:智威湯遜 - 如何將PrivateKey傳遞給「SigningCredentials」來構建SecurityTokenDescriptior?

  var securityTokenDescriptor = new SecurityTokenDescriptor() 
      { 
       Subject = new ClaimsIdentity(List), 
       Audience = "Audi", 
       Issuer = "Issr", 
       Expires = 5, 
       SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Key), Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature), 
      }; 

想知道如何將私鑰傳遞給SigningCredentials。

我有我自己的私鑰作爲標籤RSAKeyValue之間的xml文件。

謝謝。

回答

0

如果您有相關私鑰的證書 - PKCS#12那將是easy

new SigningCredentials(new X509SecurityKey(cert), "RS256"); 

因此,讓一個證書。

+0

我沒有任何證書。我只有私鑰。 – Sudhan

0

嘗試使用Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(字節[]鍵):

new SigningCredentials(new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.ASCII.GetBytes(Constants.Commons.SECRET_KEY)), Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256)));