我正在使用c#.NET创建JWT令牌。我有自己的私钥作为xml。我需要将其作为参数之一传递给函数signingCredentials。我的代码如下所示:
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 :(得分:2)
尝试使用Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(byte [] key):
new SigningCredentials(new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.ASCII.GetBytes(Constants.Commons.SECRET_KEY)), Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256)));
答案 1 :(得分:0)
答案 2 :(得分:0)
下面的示例代码
var key = new SymmetricSecurityKey(Encoding.UTF8
.GetBytes(_configuration.GetSection("AppSettings:Token").Value));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
var tokenDescrpitor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.Now.AddDays(1),
SigningCredentials = creds
};