Home > database >  JWT Token is expired or not?
JWT Token is expired or not?

Time:09-28

How to check whether the current JWT Token is expired or not in .NET Core application.

string token = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");

I got the token value using above code. Similar i need a way to find the token is expired or not.

CodePudding user response:

You can get the token expire time using this:

string accessTokenExpire = await HttpContext.GetTokenAsync("expires_at"); 

Alternatively, you as a client don't care about the expire time and instead renew the access token when the access token is rejected by the API receiving it.

Important, you are in the client, not supposed to look inside the access token.

CodePudding user response:

We can find JWT token expired or not using below code.

private readonly IHttpContextAccessor _httpContextAccessor;
var expiresAt = DateTimeOffset.Parse(await _httpContextAccessor.HttpContext.GetTokenAsync("expires_at"));
if (expiresAt != null && expiresAt < DateTime.UtcNow)
{
   //Token Expired
}
  • Related