我正在尝试运行我的应用程序,但它遇到以下错误:
System.NotSupportedException HResult = 0x80131515 Message = IDX10634:无法创建SignatureProvider.算法:'[PII默认隐藏.将IdentityModelEventSource.cs中的'ShowPII'标志设置为true以显示它.]',SecurityKey:'[PII默认隐藏.将IdentityModelEventSource.cs中的'ShowPII'标志设置为true以显示它.]'不受支持.
哪里
算法是RS256
它坚持执行这条指令: var sectoken = tokenHandler.CreateToken(tokenDescriptor);
这是什么意思?我的代码出了什么问题?我怎么解决这个问题?
这是我的代码:
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
//...
public class TokenManager
{
private string unencoded_key = "CaptainDeadpool";
private string encoded_key = "CaptainDeadpool";
//...
public TokenManager()
{
var plainTextBytes = Encoding.UTF8.GetBytes(unencoded_key);
encoded_key = Convert.ToBase64String(plainTextBytes);
}
public string CreateFromUsername(string usr, int? timer)
{
if (timer == null) { timer = 30; }
double timeadd = Convert.ToDouble(timer);
var secret = Convert.FromBase64String(encoded_key);
var tokenHandler = new JwtSecurityTokenHandler();
var actual = DateTime.UtcNow;
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, usr) }),
Expires = actual.AddMinutes(timeadd),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.RsaSha256Signature)
};
var sectoken = tokenHandler.CreateToken(tokenDescriptor);
var stringtoken = tokenHandler.WriteToken(sectoken);
return stringtoken;
}
//...
Run Code Online (Sandbox Code Playgroud)
tokenDescriptor发出错误时,这是我的内容:
Evk*_*Evk 14
不知道那个错误信息意味着什么,但我认为并不重要,因为你的代码在逻辑上是错误的.RSA是不对称算法,但您正在尝试使用SymmetricSecurityKey它.
因此,要么使用另一个(对称)签名算法(并确保您的密钥大小对此算法有效),例如:
// adjust key size
private string unencoded_key = "CaptainDeadpool!";
private string encoded_key = "CaptainDeadpool!";
// ...
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(secret),
SecurityAlgorithms.HmacSha256Signature)
Run Code Online (Sandbox Code Playgroud)
或提供有效密钥,例如:
private readonly RSA _rsa;
public TokenManager() {
// import instead of creating new, if necessary
_rsa = new RSACryptoServiceProvider(2048);
}
// ...
SigningCredentials = new SigningCredentials(
new RsaSecurityKey(_rsa),
SecurityAlgorithms.RsaSha256Signature)
Run Code Online (Sandbox Code Playgroud)
小智 5
我在使用 hmacSha256 时遇到了同样的问题。如果您的安全密钥太短,您可能会收到该错误。我增加了秘密 secureKey 的大小,这解决了我的问题。
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("You_Need_To_Provide_A_Longer_Secret_Key_Here"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4842 次 |
| 最近记录: |