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);
}Loading
Ishika TiwariPosted Feb 21, 2024, 5:01 AM
If you want to set the expiry to a specific date in the future, you can replace
If you want to set the token to never expire, you can use the DateTime.MaxValue
Note:
I suggest you don't extend the expiry of JWT tokens. Instead, you can use the concept of refresh tokens.
Aravind GovindarajPosted Feb 20, 2024, 7:12 PM
I agree with Abishek, typically your access token is supposed to be limited expiry time, if at all if you want to extend that session with token, apply refresh token concept.
Abhishek YadavPosted Feb 20, 2024, 12:04 PM
The expiration date is there for security reasons. I would possibly rather look into how to extend it when users are active, or a refresh token perhaps.