【发布时间】:2018-09-27 05:56:00
【问题描述】:
我正在尝试运行我的应用,但遇到以下错误:
System.NotSupportedException HResult=0x80131515 消息=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;
}
//...
这是我在发出错误时tokenDescriptor 的内容:
【问题讨论】:
-
设置以下标志在调试这种情况下非常有帮助。它将用实际错误替换 [PII is Hidden]。只需记住在发布到生产环境之前删除标志:IdentityModelEventSource.ShowPII = true;