【发布时间】:2020-04-30 19:45:22
【问题描述】:
我有一个要缓存的 Azure 托管标识访问令牌。我不确定检查令牌是否过期的正确方法。我正在缓存 expires_on 属性,Micrososft 将其解释为“访问令牌过期的时间跨度。日期表示为从“1970-01-01T0:0:0Z UTC”开始的秒数。
这是否意味着令牌中的 expires_on 属性已经是 Utc 格式?我不确定是否可以检查 Utc 中的日期时间并直接与之进行比较。我最大的担心是,如果我的逻辑错误,我将一遍又一遍地返回过期的令牌 - 破坏应用程序。
我目前的支票是:
var tokenExp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
tokenExp = tokenExp.AddSeconds(Int32.Parse(cachedExpiresOn)); // cachedExpiresOn is "1588350330"
if (tokenExp > DateTime.UtcNow)
{
// return cached token
} else
{
// fetch token and cache
}
【问题讨论】:
-
@JoyWang 感谢您的回复。微软只是对令牌的文档很差,他们声称令牌会在 1 小时后过期,但我看到的过期日期是 24 小时。我相信您提供的 jwt 辅助方法将正确比较 UTC 中的时间。
标签: c# asp.net .net azure active-directory