获取 ASP.NET 会话上次访问时间(或超时时间)
我试图确定给定的 ASP.NET 会话在超时之前还剩下多少时间。
如果没有现成的超时时间值,我也可以根据上次访问时间来计算它(但我也没有找到)。知道如何做到这一点吗?
I'm trying to determine how much time is left in a given ASP.NET session until it times out.
If there is no readily available time-to-timeout value, I could also calculate it from its last access time (but I didn't find this either). Any idea how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在服务器处处理请求,则超时刚刚被重置,因此剩余 20 分钟(或您配置的任何时间)。
如果您想要客户端警告,则需要创建一些 javascript 代码,该代码将在“现在”后大约 20 分钟触发。请参阅 setTimeout 方法。
我用它在请求页面 15 分钟后显示警告。它会弹出一条警告,例如“您的会话将于 {HH:mm} 到期,请保存您的工作”。使用确切的时间而不是“5 分钟内”,因为您永远不知道用户何时会看到该消息(他在警报触发后 10 分钟后是否返回计算机?)。
If you are at the server, processing the request, then the timeout has just been reset so the full 20 minutes (or whatever you configured) remain.
If you want a client-side warning, you will need to create some javascript code that will fire about 20 minutes from "now". See the setTimeout method.
I have used that to display a warning, 15 minutes after the page was requested. It pops up an alert like "your session will expire on {HH:mm}, please save your work". The exact time was used instead of "in 5 minutes" as you never know when the user will see that message (did he return to his computer 10 minutes after the alert fired?).
对于多页面解决方案,可以将上次请求时间保存在 cookie 中,并且 JavaScript 可以考虑此上次访问时间来处理警告消息或注销操作。
For multi-page solution one could save last request time in cookie, and javascript could consider this last access time for handling warning message or login out action.
我刚刚实施了一种解决方案,就像这里询问的那样,它似乎有效。我有一个
MVC
应用程序,并在我的 _Layout.chtml 页面中包含此代码,但通过将其放置在我认为的母版页中,它可以在asp.net
应用程序中工作。我通过amplify.js
插件使用本地会话存储。我使用本地会话存储,因为正如 Grieves 先生所说,可能存在这样一种情况:用户访问应用程序的方式不会导致页面刷新或重定向,但仍会重置服务器上的会话超时。然后,在用户从不导致页面刷新但仍然点击服务器从而导致会话重置的页面中,我将其放在按钮单击事件上:
使用本地会话存储允许我的 _Layout.chtml 代码看到会话已重置即使页面从未刷新或重定向。
I have just implemented a solution like the one asked about here and it seems to work. I have an
MVC
application and have this code in my _Layout.chtml page but it could work in anasp.net
app by placing it in the master page I would think. I am using local session storage via theamplify.js
plugin. I use local session storage because as Mr Grieves says there could be a situation where a user is accessing the application in a way that does not cause a page refresh or redirect but still resets the session timeout on the server.Then in a page where users never cause a page refresh but still hit the server thereby causing a reset of their session I put this on a button click event:
Using local session storage allows my _Layout.chtml code to see that the session has been reset here even though a page never got refreshed or redirected.
您可以从以下位置获取超时(以分钟为单位):
这是否足以提供信息,因为每个请求都会重置超时?不知道如何在不发出请求的情况下显示此内容?
无论如何,最好的方法是在每个请求上设置一些带有上次访问时间的会话变量。那应该提供远程信息。
You can get the timeout in minutes from:
Isn't this enough to provide the information, as the timeout is reset every request? Don't know how you want to display this without doing a request?
Anyhow, best way is on every request setting some Session variable with the last access time. That should provide the info on remote.