小编Mil*_*ard的帖子

ASP.NET Core 3.1 JWT 签名在使用 AddJwtBearer() 时无效

问题:AddJwtBearer()失败,但手动验证令牌有效。

我正在尝试使用非对称 RSA 算法生成和验证 JWT。

我可以使用此演示代码很好地生成 JWT

[HttpPost("[action]")]
[Authorize]
[ValidateAntiForgeryToken]
public async Task<IActionResult> JwtBearerToken() {
    AppUser user = await userManager.GetUserAsync(User);

    using RSA rsa = RSA.Create(1024 * 2);
    rsa.ImportRSAPrivateKey(Convert.FromBase64String(configuration["jwt:privateKey"]), out int _);
    var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256);

    var jwt = new JwtSecurityToken(
        audience: "identityapp",
        issuer: "identityapp",
        claims: new List<Claim>() {new Claim(ClaimTypes.NameIdentifier, user.UserName)},
        notBefore: DateTime.Now,
        expires: DateTime.Now.AddHours(3),
        signingCredentials: signingCredentials
    );

    string token = new JwtSecurityTokenHandler().WriteToken(jwt);

    return RedirectToAction(nameof(Index), new {jwt = token});
}
Run Code Online (Sandbox Code Playgroud)



我还可以使用下面的演示代码验证令牌及其签名

[HttpPost("[action]")]
[ValidateAntiForgeryToken]
public IActionResult JwtBearerTokenVerify(string …
Run Code Online (Sandbox Code Playgroud)

c# encryption-asymmetric jwt asp.net-core jwt-auth

7
推荐指数
1
解决办法
3841
查看次数

标签 统计

asp.net-core ×1

c# ×1

encryption-asymmetric ×1

jwt ×1

jwt-auth ×1