JWT Token Expiration time failing .net core [duplicate]
Asked Answered
H

1

8

I am trying to implement Token Based Authentication through refresh tokens and JWT in .NET Core 2.1.

This is how I am implementing the JWT Token:

Startup.cs

services.AddAuthentication(option =>
    {
        option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    }).AddJwtBearer(options =>
    {
        options.SaveToken = true;
        options.RequireHttpsMetadata = true;
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidAudience = Configuration["Jwt:Site"],
            ValidIssuer = Configuration["Jwt:Site"],
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SigningKey"]))
        };

        options.Events = new JwtBearerEvents
        {
            OnAuthenticationFailed = context =>
            {
                if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
                {
                    context.Response.Headers.Add("Token-Expired", "true");
                }
                return Task.CompletedTask;
            }
        };
    });

Token Generation:

var jwt = new JwtSecurityToken(
                issuer: _configuration["Jwt:Site"],
                audience: _configuration["Jwt:Site"],
                expires: DateTime.UtcNow.AddMinutes(1),
                signingCredentials: new SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256)
                );

        return new TokenReturnViewModel()
        {
            token = new JwtSecurityTokenHandler().WriteToken(jwt),
            expiration = jwt.ValidTo,
            currentTime = DateTime.UtcNow
        };

I am getting he correct values in Response.

enter image description here

But after a minute I set the same token for authorization in Postman and it works. If the token has expired it shouldn't.

I am using bearer tokens as authentication.

What am I doing wrong? Need direction.

Herl answered 13/3, 2019 at 19:45 Comment(3)
Have you checked with local date time value rather UTC value?Matador
I have local time gives the following problem: - expires takes the UTC time automatically irrespective of what I use.Herl
@AbhilashGopalakrishna Use UTC time with token always, instead localAddition
A
41

There is a token validation parameter called ClockSkew, it gets or sets the clock skew to apply when validating a time. The default value of ClockSkew is 5 minutes. That means if you haven't set it, your token will be still valid for up to 5 minutes.

If you want to expire your token on the exact time; you'd need to set ClockSkew to zero as follows,

options.TokenValidationParameters = new TokenValidationParameters()
{
    //other settings
    ClockSkew = TimeSpan.Zero
};

Another way, create custom AuthorizationFilter and check it manually.

var principal = ApiTokenHelper.GetPrincipalFromToken(token);
var expClaim = principal.Claims.First(x => x.Type == "exp").Value;
var tokenExpiryTime = Convert.ToDouble(expClaim).UnixTimeStampToDateTime();
if (tokenExpiryTime < DateTime.UtcNow)
{
  //return token expired
} 

Here, GetPrincipalFromToken is a custom method of the ApiTokenHelper class, and it will return the ClaimsPrincipal value that you've stored while issuing a token.

Addition answered 14/3, 2019 at 5:46 Comment(2)
super annoying breaking change from .net framework :(Selfpropulsion
Any additional documentation for this ? 300 secs is quite a high number.Verbid

© 2022 - 2024 — McMap. All rights reserved.