【问题标题】:JWT error IDX10634: Unable to create the SignatureProvider C#JWT 错误 IDX10634:无法创建 SignatureProvider C#
【发布时间】: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;

标签: c# jwt


【解决方案1】:

不知道该错误消息是什么意思,但我认为这并不重要,因为您的代码在逻辑上是错误的。 RSA 是不对称算法,但您正在尝试使用SymmetricSecurityKey

所以要么使用另一种(对称)签名算法(并确保您的密钥大小对此算法有效),例如:

// adjust key size
private string unencoded_key = "CaptainDeadpool!";
private string encoded_key = "CaptainDeadpool!";
// ...
SigningCredentials = new SigningCredentials(
    new SymmetricSecurityKey(secret), 
    SecurityAlgorithms.HmacSha256Signature)

或者提供有效的密钥,例如:

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)

【讨论】:

  • 我尝试了第一个解决方案,但并没有解决问题。第二个效果很好!只是一个简单的问题,......“(2048)”是什么?我可以改用我的密钥吗?
  • 2048 是私钥的大小。不对称算法使用 2 个密钥 - 公钥和私钥(因此您不能按原样使用您的密钥)。我将其设置为 2048,因为这是 SecurityAlgorithms.RsaSha256Signature 的要求。
  • 要使第一个“解决方案”起作用,您需要按照我所说的调整您的秘密大小。例如,将您的密码更改为 CaptainDeadpool!(多一个字符)
  • @Evk 至少对我来说,短“秘密”确实是个问题。
【解决方案2】:

我在使用 hmacSha256 时遇到了同样的问题。如果您的安全密钥太短,您可能会收到该错误。我增加了秘密安全密钥的大小,这解决了我的问题。

 var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("You_Need_To_Provide_A_Longer_Secret_Key_Here"));

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2015-04-01
    相关资源
    最近更新 更多