如何为 JWT Token 设置长有效期?

问题描述 投票:0回答:1
 private string GenerateJSONWebToken(pswd user)
 {
     var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
     var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
     var token = new JwtSecurityToken(_config["Jwt:Issuer"],
         _config["Jwt:Issuer"],
         null,
         expires: DateTime.Now.AddYears(100),
         signingCredentials: credentials);
            
     return new JwtSecurityTokenHandler().WriteToken(token);
  }

这里我将令牌的过期时间添加到 100 年后。 我收到这个错误 enter image description here

生成的token是正确的,我调试了一下,检查了有效期,是100年后。但生成的令牌不起作用,无法从其预期的 Web api 访问数据。 我可以添加的最大有效期为 10 年,直到生成的令牌完美运行为止。它能够通过 200 响应代码正确获取数据。

如何设置 jwt 令牌的生命周期有效性以及为什么即使我将过期时间设置为 100 年或更长,也会收到“无过期”错误。

c# asp.net asp.net-core authentication jwt
1个回答
0
投票

出于安全原因设置了有效期。我可能更愿意研究如何在用户处于活动状态时扩展它,或者可能是刷新令牌。

© www.soinside.com 2019 - 2024. All rights reserved.