c# - 无法理解 IdentityTokenLifetime 以及我的 JWT 持续多长时间
问题描述
我无法理解 JWT 令牌的寿命。
- IdentityTokenLifetime(身份令牌的生命周期,以秒为单位(默认为 300 秒 / 5 分钟)
= 120 / 60 = 2 分钟
的目的是IdentityTokenLifetime
什么?
- AccessTokenLifetime(访问令牌的生命周期,以秒为单位(默认为 3600 秒/1 小时)
= 120 / 60 = 2 分钟
- SlidingRefreshTokenLifetime(刷新令牌的滑动生命周期,以秒为单位。默认为 1296000 秒/15 天)
= 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"]
}
}
};
解决方案
从您的示例中,一旦身份验证成功,将创建以下令牌:
- 在 300 秒后过期的刷新令牌。值TokenExpiration.Absolute表示 Refresh Token 不会被刷新。这通常太短了一个值。刷新令牌通常持续数天。刷新令牌过期后,将无法再刷新令牌,用户将需要再次进行身份验证。
- 120 秒后过期的访问令牌。如果刷新令牌尚未过期,则会创建一个新的访问令牌。
- 120 秒后过期的身份令牌。如果刷新令牌尚未过期,则会创建一个新的身份令牌。
要获得每个令牌的生命周期(以分钟为单位),请将秒数除以 60。
推荐阅读
- kubernetes - Cert-manager 在升级到 AKS 1.20.7 后停止更新 Let'S Encrypt 证书
- python - 使用 groupby 时不显示 Pandas 列
- conv-neural-network - CNN 中的最小和平均池化
- impala - [ImpalaJDBCDriver](500312) 获取数据行时出错:null
- python - 如何在 Google colab 中更改 pytorch 版本
- python - “mat1 和 mat2 形状不能相乘(448x7 和 576x10)”,同时尝试执行 Layer Wise 相关传播
- c++ - 创建一个程序,该程序具有 3 个不同的变量,其数据类型为整数
- ruby - rvm install 2.6.7 在 Apple M1 mac 上总是失败
- google-bigquery - SQL中的数组排名和分组依据
- reactjs - 过渡到 React.lazy 加载的组件时闪烁