問題描述
我正在嘗試運行我的應用,但遇到以下錯誤:
I'm trying to run my app but it get stuck with the following error:
System.NotSupportedException HResult=0x80131515 消息=IDX10634:無法創(chuàng)建 SignatureProvider.算法:'[PII 被隱藏默認.將 IdentityModelEventSource.cs 中的ShowPII"標志設置為 true顯示它.]', SecurityKey: '[PII 默認隱藏.設置IdentityModelEventSource.cs 中的ShowPII"標志為 true 以顯示它.]'不支持.
System.NotSupportedException HResult=0x80131515 Message=IDX10634: Unable to create the SignatureProvider. Algorithm: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]', SecurityKey: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]' is not supported.
在哪里
算法是RS256
卡在執(zhí)行這條指令:var sectoken = tokenHandler.CreateToken(tokenDescriptor);
這是什么意思?我的代碼出了什么問題?我該如何解決這個問題?
What does it mean? What went wrong in my code? How can I solve this?
這是我的代碼:
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;
}
//...
這是我發(fā)出錯誤時的 tokenDescriptor
的內容:
Here's my tokenDescriptor
's content while issuing the error:
推薦答案
不知道該錯誤消息是什么意思,但我認為沒關系,因為您的代碼在邏輯上是錯誤的.RSA 是非對稱算法,但您正在嘗試將 SymmetricSecurityKey
與它一起使用.
No idea what that error message means, but it doesn't matter I think, because your code is logically wrong. RSA is assymetric algorithm, but you are trying to use SymmetricSecurityKey
with it.
所以要么使用另一種(對稱)簽名算法(并確保您的密鑰大小對此算法有效),例如:
So either use another (symmetric) signature algorithm (and ensure that your key size is valid for this algorithm), for example:
// adjust key size
private string unencoded_key = "CaptainDeadpool!";
private string encoded_key = "CaptainDeadpool!";
// ...
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(secret),
SecurityAlgorithms.HmacSha256Signature)
或者提供有效的密鑰,例如:
Or provide valid key, for example:
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)
這篇關于JWT 錯誤 IDX10634:無法創(chuàng)建 SignatureProvider C#的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!