LifetimeValidator lifetimeValidator = (DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters) =>
{
if (expires != null && notBefore != null)
{
if (DateTime.UtcNow < expires.Value.ToUniversalTime() & DateTime.UtcNow > notBefore.Value.ToUniversalTime())
{
return true; // Still valid
}
}
return false; // Expired
};
if (_authToken != null & tokenHandler.CanReadToken(_authToken) & _authToken?.Length == 348)
{
var jwtSecurityToken = tokenHandler.ReadJwtToken(_authToken);
SecurityToken validatedToken;
IPrincipal principal = tokenHandler.ValidateToken(_authToken, GetValidationParameters(), out validatedToken);
if (lifetimeValidator(validatedToken.ValidFrom, validatedToken.ValidTo, jwtSecurityToken, GetValidationParameters()))
{
if(principal?.Identity?.Name != null)
{
return true; // Token is valid
}
}
else
{
return false; // Token is expired
}
}
return false; // Token is damaged
}