Good day,
I'm trying to check if an token expiration date is already expired. I'm using JWT in generating my token. I'm able to check in my javascript environment if my token is still valid by using this code.
if (decodedToken.exp < new Date().getTime() / 1000) {
alert("Session expired);
}
// decodedToken.exp returns a value like "1556797243"
But my problem is, I don't know how to validate using C# code if the token is already expired.
I tried something like this,
// suppose I have a value of "1556797243"
var expirationDate = DateTime.ParseExact(expiration, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
But it doesn't work.
.NET has a built-in way to parse a Unix timestamp, but it's on DateTimeOffset, not DateTime, as you might expect:
var expirationTime = DateTimeOffset.FromUnixTimeSeconds(expiration).UtcDateTime;
If expiration is a string you'd need to parse it into a long first:
var expirationTime = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expiration)).UtcDateTime;