gpt4 book ai didi

asp.net - JwtSecurityToken 理解与异常

转载 作者:行者123 更新时间:2023-12-04 06:14:31 24 4
gpt4 key购买 nike

我对 JwtSecurityTokens 还很陌生,我试着去了解它的不同方面,甚至是整个 claimsidentityclaimprincipal ,但那是另一回事了。

我尝试在 中生成 token C# 通过使用以下代码:

private const string SECRET_KEY = "abcdef";
private static readonly SymmetricSecurityKey SIGNING_KEY = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET_KEY));

public static string GenerateToken(string someName)
{
var token = new JwtSecurityToken(
claims: new Claim[]
{
new Claim(ClaimTypes.Name, someName),
},
notBefore: new DateTimeOffset(DateTime.Now).DateTime,
expires: new DateTimeOffset(DateTime.Now.AddMinutes(60)).DateTime,
signingCredentials: new SigningCredentials(SIGNING_KEY, SecurityAlgorithms.HmacSha256)
);

return new JwtSecurityTokenHandler().WriteToken(token);
}

I followed a tutorial on Youtube, but I'm not sure I understand the different parts in the JwtSecurityToken. In addition, when I execute the code through a controller just to try to return a token, it returns an error, saying: "IDX10603: Decryption failed. Keys tried: '[PII is hidden]'".



任何帮助表示赞赏。

最佳答案

算法 HS256 需要 SecurityKey.KeySize大于 128 位,而您的 key 只有 48。通过添加至少 10 个符号来扩展它。
至于“PII is hidden”部分,它是作为 GDPR 合规工作的一部分完成的,目的是在日志中隐藏任何堆栈或变量信息。您应该通过以下方式启用其他详细信息:

IdentityModelEventSource.ShowPII = true;

关于asp.net - JwtSecurityToken 理解与异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52588240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com