JWT error IDX10634: Unable to create the SignatureProvider C#(JWT 错误 IDX10634:无法创建 SignatureProvider C#)
问题描述
我正在尝试运行我的应用,但遇到以下错误:
I'm trying to run my app but it get stuck with the following error:
System.NotSupportedException HResult=0x80131515 消息=IDX10634:无法创建 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
卡在执行这条指令: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;
}
//...
这是我发出错误时的 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:无法创建 SignatureProvider C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JWT 错误 IDX10634:无法创建 SignatureProvider C#


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01