c# - Identity Server (OpenID Connect) 混合流程:需要用户使用刷新令牌保持登录状态 5 年
问题描述
我正在使用 Identity Server 3 OpenID Connect 创建一个具有长期访问权限的混合客户端(允许用户保持登录状态 5 年而无需输入其凭据)
我已经定义了一个内存配置存储客户端,如下所示:
new Client
{
Enabled = true,
ClientId = MyClientApp.Id,
ClientUri = MyClientApp.Uri,
ClientName = MyClientApp.Name,
Flow = Flows.Hybrid,
AllowAccessToAllScopes = true,
IdentityTokenLifetime = 300,
AccessTokenLifetime = 3600,
RefreshTokenExpiration = TokenExpiration.Absolute,
AbsoluteRefreshTokenLifetime = (int)TimeSpan.FromDays(1825).TotalSeconds,
RefreshTokenUsage = TokenUsage.OneTimeOnly,
UpdateAccessTokenClaimsOnRefresh = true,
RequireConsent = false,
RedirectUris = new List<string>
{
MyClientApp.Uri
},
PostLogoutRedirectUris = new List<string>
{
MyClientApp.Uri
},
ClientSecrets = new List<Secret>
{
new Secret(MyClientApp.Secret.Sha256())
}
},
在哪里:
MyClientApp.Id = test.client
MyClientApp.Uri = https://testclient.trx.com
MyClientApp.Name = My Test Client
访问令牌将在 3600 秒(1 小时)后过期
身份令牌将在 300 秒(5 分钟)后过期,
刷新令牌(绝对)将在 5 年后到期
这是定义满足我要求的客户的正确方法吗?
Requirements:
User Signs in (authenticates) one time
User will remain signed in without the session expiring in 5 years
User will need to enter their credentials again after 5 years
我会很感激任何帮助
谢谢你
解决方案
因此,查看这个看起来它是 IdentityServer3 的正确设置,以获得您正在寻找的结果。
在 IS4 中,您将AllowOfflineScope
设置GrantType
- 在 IS3 中Flow
,AllowAccessToAllScopes
您已设置,生命周期看起来很好 - 应该每小时解析一次新的访问令牌,刷新令牌将执行 5 年。
编辑:通过提供有关在 SPA 中使用 AngularJS 的附加信息,这会改变一些事情,在 Angular 4.3 版本之前似乎不支持刷新令牌,在 4.3 之后,您可以使用 HttpInterceptor 来利用刷新令牌。
关于Flow
客户端,SPA 只能使用该Implicit
类型。
.
使用 oidc-token-manager 和静默更新:
正如您所提到的,使用oidc-token-manager
一个选项是使用该Silent renew
功能并在 IdentityServerExpireTimeSpan
中配置CookieOptions
- 这允许单页应用程序根据客户端上的 cookie 静默更新会话 - 当然,唯一的缺点是客户端清除他们的 cookie 然后需要再次登录。
本IdentityServer 教程的第 3 部分详细介绍了如何设置静默更新,并且所需要的只是延长 cookie 的生命周期。
.
将 HttpInterceptors 与刷新令牌一起使用:
还提到的 AngularJS 4.3+ 中支持的 HttpInterceptor 是我真正没有使用经验的东西 - 尽管有很多指南可以用它来实现刷新令牌,就像这个例子一样。
推荐阅读
- python - 无法将字符串值转换为浮点值
- python - Flask-sqlalchemy 限制 Oracle 的偏移分页性能
- frameworks - ValidationGroupDirective 错误 Abp 框架
- databricks - 如何使用 API 或类库读取 Azure Databricks 输出
- c# - C#等待从日志文件中获取文本,因为它正在被写入
- r - 当第一个 ID 全部丢失时,在 sapply 中运行函数会返回错误结果
- c# - wpf c# sql:我可以在同一个存储过程中选择具有聚合值的表吗?
- angular - 如何使用 Angular 日历移动日历日期
- visual-studio-code - 为什么按字母'o'时会在vs code中自动打开搜索扩展?
- r - 在ggplot2中添加手动设计的非线性线?