有没有办法在 OpenIddict 中设置不同的票证有效期?

Is there a way to have different ticket expiry lengths in OpenIddict?

我有一个使用 OpenIddict 进行令牌授权(访问和刷新令牌)的应用程序,总的来说,它运行良好。问题是我的用例有多个应用程序类型(网络和移动)使用相同的授权服务器。我希望不同类型的到期时间不同(可能使用不同的令牌端点),但我想不出一种方法来覆盖使用 SetAccessTokenLifetime 和 SetRefreshTokenLifetime 设置的值。有办法吗?

目标是为 Web 应用程序提供更长的访问令牌长度,并让用户在它们过期时重定向到登录(合理长的过期时间,例如几小时)。在移动端,我想使用刷新令牌让用户保持登录状态。最佳实践似乎表明,在移动设备上,我应该有一个非常短的令牌到期时间(例如分钟)和一个很长的刷新令牌到期时间。

谢谢, 杰森

I'd like to have different expiry times for the different types (probably using different token endpoints) but I can't figure out a way to override the values set with SetAccessTokenLifetime and SetRefreshTokenLifetime. Is there a way to do this?

您可以使用专用 ClaimsPrincipal 扩展直接从您的授权端点操作覆盖全局令牌过期值:

principal.SetAccessTokenLifetime(TimeSpan.FromMinutes(30));
principal.SetAuthorizationCodeLifetime(TimeSpan.FromMinutes(1));
principal.SetIdentityTokenLifetime(TimeSpan.FromMinutes(30));
principal.SetRefreshTokenLifetime(TimeSpan.FromDays(2));