JwtSecurityToken doesn't expire when it should

后端 未结 6 1940
耶瑟儿~
耶瑟儿~ 2021-02-02 07:55

I am currently using the JwtSecurityToken class in System.IdentityModels.Tokens namespace. I create a token using the following:

DateTime expires = DateTime.Utc         


        
6条回答
  •  执笔经年
    2021-02-02 08:30

    The problem is related ClockSkew. Normally, the validation libraries (at least the MS one) compensate for clock skew. ClockSkew default value is 5 minutes. See some answer here

    You can change ClockSkew in TokenValidationParameters:

    var tokenValidationParameters = new TokenValidationParameters
    {
        //...your setting
    
        // set ClockSkew is zero
        ClockSkew = TimeSpan.Zero
    };
    
    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        TokenValidationParameters = tokenValidationParameters
    });
    

    Happy coding!

提交回复
热议问题