首页 > 解决方案 > 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

我会很感激任何帮助

谢谢你

标签: c#.netopenididentityserver3

解决方案


因此,查看这个看起来它是 IdentityServer3 的正确设置,以获得您正在寻找的结果。

在 IS4 中,您将AllowOfflineScope设置GrantType- 在 IS3 中FlowAllowAccessToAllScopes您已设置,生命周期看起来很好 - 应该每小时解析一次新的访问令牌,刷新令牌将执行 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 是我真正没有使用经验的东西 - 尽管有很多指南可以用它来实现刷新令牌,就像这个例子一样


推荐阅读