【发布时间】:2019-03-06 09:27:12
【问题描述】:
我对@987654321@ 还很陌生,我试图了解它的不同方面,以及整个claimsidentity 和claimprincipal,但那是另一回事了。
我尝试使用以下代码在 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);
}
我遵循了 Youtube 上的教程,但我不确定我是否理解 JwtSecurityToken 中的不同部分。另外,当我执行 通过控制器的代码只是为了尝试返回一个令牌,它 返回一个错误,说:“IDX10603:解密失败。密钥尝试: '[PII 被隐藏]'"。
感谢任何帮助。
【问题讨论】:
标签: asp.net .net asp.net-core jwt