同步 OWIN Cookie 过期(滑动)和会话超时

Syncing OWIN Cookie Expiration (sliding) and session timeout

背景

我有一个使用会话存储和 OWIN cookie 身份验证的半单页应用程序(登录后所有服务器交互都通过 ajax 调用在一个页面上进行)。会话和 cookie 过期都设置为 15 分钟,并且 cookie 配置为使用滑动过期。

每次与服务器交互时,客户端上的计时器都会重置为 15 分钟。如果 14 分钟内没有 activity 被记录,将显示一个带有滚动条的模式,提醒用户会话即将超时。

问题

我们 运行 遇到一个问题,在某些情况下我们的会话比 OWIN cookie 还长。

根据 CookieAuthenticationOptions class:

The SlidingExpiration is set to true to instruct the middleware to re-issue a new cookie with a new expiration time any time it processes a request which is more than halfway through the expiration window.

我们有这样的场景,用户在“过期过半window”之前使用应用程序,然后让应用程序闲置几分钟。当用户看到会话过期通知时,他们尝试更新他们的会话。但由于 OWIN cookie 从未更新过,应用程序超时并将他们重定向到登录页面(过早地根据过期通知所说的内容)。

这个support article正好说明了我们的情况:

Let us take an example: If the logon page is accessed at 5:00 00:00:00 PM, it should expire at 5:10 00:00:00 PM if the timeout attribute is 10 and the slidingExpiration attribute is set to TRUE. Now, if any Web page is browsed again at 5:05 00:00:00 PM, the cookies and ticket time-out period will be reset to 5:15 00:00:00 PM.

Note If the Web page is accessed before half of the expiration time passes, the ticket expiration time will not be reset. Fore example, if any Web page is accessed again at 5:04 00:00:00 PM, the cookies and ticket timeout period will not be reset.

Question/Comments

以前有人 运行 参与过这个吗?我们如何避免它?调整 OWIN 超时只会延长问题。

我忍不住认为这是 OWIN 框架中一个可怕的设计缺陷,或者我们没有正确使用它。

感谢任何帮助。谢谢

我们最终实施的解决方案是使用我们的保持活动逻辑的变通方法。我真的希望他们能以更传统的方式实施 "sliding expiration",但现在必须这样做。

解决方案:

Only send keep-alive requests to the server until AFTER half of the OWIN cookie life passes. When the keep-alive is sent, the cookie will be renewed and this keeps the session and cookie timeout in sync.