Technical questionAccepted answer
How to get "exp" from jwt token and compare with it current time to check if token is expired
user584018
Jan 27
0 views1
I am using System.IdentityModel.Tokens.Jwt package and the below code decoding the jwt token, but it won't give exp value?
var handler = new JwtSecurityTokenHandler();
var decodedValue = handler.ReadJwtToken("token");
How to get exp and compare it with the current DateTime to calculate token is expired or not?
Update:
I am using Azure.Core.AccessToken where I have the below property,
public DateTimeOffset ExpiresOn
{
get;
}
1 answer
Accepted answer · original discussion
Sinan ILYAS
PermalinkDec 6
This is how I check if a token is valid.
using System.IdentityModel.Tokens.Jwt;
private bool IsValid(string token)
{
JwtSecurityToken jwtSecurityToken;
try
{
jwtSecurityToken = new JwtSecurityToken(token);
}
catch (Exception)
{
return false;
}
return jwtSecurityToken.ValidTo > DateTime.UtcNow;
}
3 question comments
Use comments to ask for clarification. Post a solution as an answer.
ProgrammingLlamaPermalink
Jan 27
Wait, are you just asking for something like
if (token.ExpiresOn > DateTimeOffset.UtcNow) or something?user584018Permalink
Jan 27
@Llama, I have azure ad token which generates through azure managed identity and looks like it's different than jwt token. it's a
Azure.Core.AccessToken tokenuser584018Permalink
Jan 27