jwt token expiration time (asp.net core)

前端 未结 3 1742
青春惊慌失措
青春惊慌失措 2021-01-27 07:21

I\'d like to increase the lifetime of JWT token but I can\'t.

I tried googling the matter and found references to JwtBearerOptions.TokenValidationParameters.ClockS

相关标签:
3条回答
  • 2021-01-27 07:47

    expiration value undefined in token descriptor.

    var tokenDescriptor = new SecurityTokenDescriptor
    {
       Issuer = _config.GetValidIssuer(),
       Audience = _config.GetValidAudience(),
       SigningCredentials = new SigningCredentials(_config.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256),
       Subject = new ClaimsIdentity(new[]
       {
          new Claim(ClaimTypes.Name, user.UserName),
          new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
       }),
    
       // expiration time here...
       Expiration = _config.GetExpiration() // etc
    };
    
    0 讨论(0)
  • 2021-01-27 07:53
    var token = new JwtSecurityToken(_config["Jwt:Issuer"], _config["Jwt:Issuer"],  
     claims, expires: DateTime.Now.AddMinutes(120) 
    
    0 讨论(0)
  • 2021-01-27 07:59

    There' a typo in Bayram's answer, so I think I should post mine.

    The property Expiration doesn't exist in SecurityTokenDescriptor. It's DateTime? Expires.

    DateTime expires = input.RememberMe ? DateTime.UtcNow.AddDays(5) : DateTime.UtcNow.AddMinutes(20);
    
    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Expires = expires,
        ...
    

    Works perfectly!

    0 讨论(0)
提交回复
热议问题