Reputation: 43
Based on Get expire time of OAuth session I create a simple method to retreive expiration date. I used both of answers and get different results:
var tokenResponse = await httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
Address = tokenEndpoint,
ClientId = client,
ClientSecret = secret,
GrantType = "client_credentials",
Scope = scope,
});
var currDateTime = DateTime.Now;
var tokenExpireDate1 = currDateTime.AddSeconds(tokenResponse.ExpiresIn);
var handler = new JwtSecurityTokenHandler();
var jwtToken = handler.ReadToken(tokenResponse.AccessToken) as JwtSecurityToken;
var tokenExpireDate2 = jwtToken.ValidTo;
Why dates are different? And why second date is less then current date?
Upvotes: 2
Views: 4300
Reputation: 16498
DateTime.Now
is the time in the system's local time zone while jwtToken.ValidTo
is the UTC time.
Just convert the tokenExpireDate1 to UTC time you will find the two date are the same.
DateTime tokenExpireDate11 = TimeZoneInfo.ConvertTimeToUtc(tokenExpireDate1);
Upvotes: 3