如何显示足够智能的会话超时警告来处理多个打开的浏览器或选项卡
我已经使用 JavaScript 实现了会话超时警告,它只是询问用户是否要延长会话或注销。 问题在于,这是针对 Intranet 门户的,高级用户通常会同时打开多个浏览器窗口或选项卡来访问应用程序。 目前,系统会提示他们即将从每个浏览器窗口注销。 如何使代码更智能地检测到他们正在主动使用另一个浏览器会话?
I have implemented a session timeout warning using javascript that simply asks the user if they want to extend their session or logout. The problem is that this is for an intranet portal where power users will often have several browser windows or tabs open at the same time to the application. Currently, they will be prompted that they are about to be logged out from every browser window. How can I make the code smarter to detect that they are actively using another browser session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用 Ajax 检查服务器上的会话状态并跟踪用户拥有的所有打开的会话/窗口。 然后,您就可以仅针对可用会话之一发出注销警告。
回应您的评论:
不要使用内置会话机制,使用服务器端持久数组或数据库日志设计您自己的机制。
不,HTTP 请求中没有任何内容告诉您打开了多少个浏览器,但您可以在用户打开每个浏览器窗口时分配自己的 sessionID cookie。 对服务器进行 Ajax 调用,查看用户是否超时,如果您是会话日志中的最低(或最后)条目,那么您就是收到警告的浏览器。
You'd have to check the session state on the server using Ajax and keep track of all the open sessions/windows the user has. You'd then be able to target only one of the available sessions with the log out warning.
In response to your comment:
Don't use the built-in session mechanism, devise your own using an server-side presistent array or a database log.
No, nothing in the HTTP request tells you how many browsers are open, but you can assign your own sessionID cookie as the user opens each browser window. Make an Ajax call to the server, see if the user has timed-out, and if you're the lowest (or last) entry in the session log then you're the browser that gets the warning.
您不能指望所有选项卡/窗口都属于同一会话,因为它们可能会生成并包含在单独的进程中,而您对此没有太多控制权。
但是,如果您的代码引用 Javascript cookie,您可以通过回发(同步或异步 AJAX)检查伪会话状态。 但随后您就依赖于用户浏览器上启用的 cookie。
You can't count on all tabs/windows to be part of the same Session, because they could be spawned and contained within separate processes and you don't have much control over that.
But if your code references a Javascript cookie, you can check your pseudo-session state via a postback (synchronous or asynchronous AJAX). But then you're depending on cookies being enabled on the user's browser.
这行得通吗?
存储 Javascript cookie 并检查以确定会话是否已在另一个选项卡中扩展?
看起来这确实有效...
Would this work?
Store a Javascript cookie and check that to determine if the session has been extended in another tab?
looks like this does work...
安装@ng-idle
@ng-idle 可通过 NPM 获得。 通过运行安装它:
设置您的应用程序模块
使用 then 导入 Ng2IdleModule
打开 src/app/app.module.ts 并在 component.ts 中
Install @ng-idle
@ng-idle is available via NPM. Install it by running:
Set up your application module
Open src/app/app.module.ts and import the Ng2IdleModule using
then in component.ts