Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP NET Core JWT authentication allows expired tokens

For some reason my RESTful app allows requests from Angular client with expired token for some time. Generating token:

private async Task<string> GenerateJwtToken(ApplicationUser user)
{
    var claims = new List<Claim>
    {
        new Claim(JwtRegisteredClaimNames.Sub, user.Email),
        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
        new Claim(ClaimTypes.NameIdentifier, user.Id)
    };
    claims.AddRange(await _userManager.GetClaimsAsync(user));
    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration.GetSection("SigningKey").Value));
    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
    var expires = 
        DateTime.Now.AddSeconds(10);
        //DateTime.Now.AddDays(Convert.ToDouble(_configuration["ExpireDays"]));

    var token = new JwtSecurityToken(
        issuer: _configuration["Issuer"],
        audience: _configuration["Audience"],
        claims: claims,
        expires: expires,
        signingCredentials: creds);
    return new JwtSecurityTokenHandler().WriteToken(token);
}

on the client before request I log expiration time, now and if now more than expiration time. log of two successful requests, however the last one supposed to be failed

t: Tue Sep 18 2018 08:53:43 GMT+0300 (Moscow Standard Time) credentials-service.ts:101

now: Tue Sep 18 2018 08:53:41 GMT+0300 (Moscow Standard Time) credentials-service.ts:102

false

true means expired

credentials-service.ts:100 t: Tue Sep 18 2018 08:53:43 GMT+0300 (Moscow Standard Time)

credentials-service.ts:101 now: Tue Sep 18 2018 08:58:01 GMT+0300 (Moscow Standard Time)

credentials-service.ts:102

true

I only got refused after 5-6 minutes for some reason instead of 10 seconds.

like image 311
Alexander Kozachenko Avatar asked Sep 18 '18 06:09

Alexander Kozachenko


People also ask

What happens when JWT token expires .NET core?

Option 1 after token expiration, the user re-login by passing username and password and get the new access token. Option 2 using Refresh token re-generate new JWT access token and consume the secured API (without re-login).

How do you handle expired JWT tokens?

So in summary when authorization is successful you need to issue two token ACCESS_TOKEN and REFRESH_TOKEN. When ACCESS_TOKEN expires you need to call another api with REFRESH_TOKEN to get new ACCESS_TOKEN. The client application can get a new access token as long as the refresh token is valid and unexpired.

What happens if JWT token expires?

The JWT access token is only valid for a finite period of time. Using an expired JWT will cause operations to fail.


1 Answers

Where you define your TokenValidationParameters in Startup.cs, give the property ClockSkew a TimeSpan value of zero:

ClockSkew = TimeSpan.Zero, e.g:

new TokenValidationParameters
                {
                    IssuerSigningKey = signingKey,
                    ValidIssuer = issuer,
                    ValidAudience = audience,
                    ValidateLifetime = true,

                    ClockSkew = TimeSpan.Zero
                };

The reason for this is that ClockSkew has a default value of 5 minutes

like image 173
MattjeS Avatar answered Sep 28 '22 03:09

MattjeS