首页 > 解决方案 > 无法理解 IdentityTokenLifetime 以及我的 JWT 持续多长时间

问题描述

我无法理解 JWT 令牌的寿命。

= 120 / 60 = 2 分钟

的目的是IdentityTokenLifetime什么?

= 120 / 60 = 2 分钟

= 300 / 60 = 5 分钟

从奇怪的总结评论信息来看,我真的不明白 JWT 令牌在几分钟内能存活多久。

public static IEnumerable<Client> GetClients(IConfiguration configuration) =>
    new List<Client>
    {
        new()
        {
            ClientName = configuration["AuthConfiguration:ClientName"],
            ClientId = configuration["AuthConfiguration:ClientId"],
            ClientSecrets = { new Secret(configuration["AuthConfiguration:ClientSecret"].Sha256()) },

            AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
            AccessTokenType = AccessTokenType.Jwt,
            AllowOfflineAccess = true,

            AccessTokenLifetime = 120,
            IdentityTokenLifetime = 120,
            UpdateAccessTokenClaimsOnRefresh = true,
            SlidingRefreshTokenLifetime = 300,
            RefreshTokenExpiration = TokenExpiration.Absolute,
            RefreshTokenUsage = TokenUsage.OneTimeOnly,
            AlwaysSendClientClaims = true,

            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.OfflineAccess,
                configuration["AuthConfiguration:ApiName"]
            }
        }
    };

标签: c#identityserver4identityasp.net-core-5.0

解决方案


从您的示例中,一旦身份验证成功,将创建以下令牌:

  1. 在 300 秒后过期的刷新令牌。值TokenExpiration.Absolute表示 Refresh Token 不会被刷新。这通常太短了一个值。刷新令牌通常持续数天。刷新令牌过期后,将无法再刷新令牌,用户将需要再次进行身份验证。
  2. 120 秒后过期的访问令牌。如果刷新令牌尚未过期,则会创建一个新的访问令牌。
  3. 120 秒后过期的身份令牌。如果刷新令牌尚未过期,则会创建一个新的身份令牌。

要获得每个令牌的生命周期(以分钟为单位),请将秒数除以 60。


推荐阅读