判断AccessToken是否过期

Determine if AccessToken is expired

我正在使用带有刷新令牌的混合流程。我想限制从我的 Web 服务器到我的 Auth/Resource 服务器的调用,即来自资源服务器的未经授权的错误和访问令牌的不必要更新

问题: 访问令牌是否有到期日期或将到期或颁发日期时间添加到访问令牌的方法?我想在刷新令牌之前用它来测试。

我想在网络服务器上进行验证。我只需要访问令牌上的发布日期

我知道到期日不是完整的证明,令牌可能仍然无效,但我可以在出现这种情况时进行处理。

谢谢

客户端配置允许设置以下有关访问令牌生命周期的属性:

AccessTokenLifetime:访问令牌的生命周期(以秒为单位)

AbsoluteRefreshTokenLifetime:刷新令牌的最长生命周期

RefreshTokenExpiration:固定时间过期(有绝对和滑动选项)

这里是关于这个的文档: http://docs.identityserver.io/en/release/reference/client.html

网络服务器是客户端。客户端可以像这样读取过期时间(这已经是访问令牌的一部分):

using System.IdentityModel.Tokens.Jwt;

public class HomeController : Controller
{

    public async Task<IActionResult> CallApiUsingUserAccessToken()
    {
        var accessToken = await HttpContext.GetTokenAsync("access_token");

        // Read expiration time
        var tokenHandler = new JwtSecurityTokenHandler();
        var jwtSecurityToken = tokenHandler.ReadJwtToken(accessToken);

        var validTo = jwtSecurityToken.ValidTo;

        // ...
    }
}

我刚刚添加了有关读取到期时间的行。 HomeController is part of the MvcClient project which is available in the 5_HybridFlowAuthenticationWithApiAccess 样本。