获取 ASP.NET 会话上次访问时间(或超时时间)

发布于 2024-08-06 12:17:05 字数 98 浏览 3 评论 0原文

我试图确定给定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剪不断理还乱 2024-08-13 12:17:05

如果您在服务器处处理请求,则超时刚刚被重置,因此剩余 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?).

寄与心 2024-08-13 12:17:05

对于多页面解决方案,可以将上次请求时间保存在 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.

挽心 2024-08-13 12:17:05

我刚刚实施了一种解决方案,就像这里询问的那样,它似乎有效。我有一个 MVC 应用程序,并在我的 _Layout.chtml 页面中包含此代码,但通过将其放置在我认为的母版页中,它可以在 asp.net 应用程序中工作。我通过 amplify.js 插件使用本地会话存储。我使用本地会话存储,因为正如 Grieves 先生所说,可能存在这样一种情况:用户访问应用程序的方式不会导致页面刷新或重定向,但仍会重置服务器上的会话超时。

$(document).ready(function () {

var sessionTimeout = '@(Session.Timeout)'; //from server at startup
amplify.store.sessionStorage("sessionTimeout", sessionTimeout);
amplify.store.sessionStorage("timeLeft", sessionTimeout);
setInterval(checkSession, 60000); // run checkSession this every 1 minute
    function checkSession() {
    var timeLeft = amplify.store.sessionStorage("timeLeft");
    timeLeft--; // decrement by 1 minute
    amplify.store.sessionStorage("timeLeft", timeLeft);
        if (timeLeft <= 10) {
        alert("You have  " + timeLeft + " minutes before session timeout. ");
                   }
     }
});

然后,在用户从不导致页面刷新但仍然点击服务器从而导致会话重置的页面中,我将其放在按钮单击事件上:

$('#MyButton').click(function (e) {

   //Some Code that causes session reset but not page refresh here
    amplify.store.sessionStorage("sessionTimeout", 60); //default session timeout
    amplify.store.sessionStorage("timeLeft", 60);


});

使用本地会话存储允许我的 _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 an asp.net app by placing it in the master page I would think. I am using local session storage via the amplify.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.

$(document).ready(function () {

var sessionTimeout = '@(Session.Timeout)'; //from server at startup
amplify.store.sessionStorage("sessionTimeout", sessionTimeout);
amplify.store.sessionStorage("timeLeft", sessionTimeout);
setInterval(checkSession, 60000); // run checkSession this every 1 minute
    function checkSession() {
    var timeLeft = amplify.store.sessionStorage("timeLeft");
    timeLeft--; // decrement by 1 minute
    amplify.store.sessionStorage("timeLeft", timeLeft);
        if (timeLeft <= 10) {
        alert("You have  " + timeLeft + " minutes before session timeout. ");
                   }
     }
});

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:

$('#MyButton').click(function (e) {

   //Some Code that causes session reset but not page refresh here
    amplify.store.sessionStorage("sessionTimeout", 60); //default session timeout
    amplify.store.sessionStorage("timeLeft", 60);


});

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.

空‖城人不在 2024-08-13 12:17:05

您可以从以下位置获取超时(以分钟为单位):

Session.Timeout

这是否足以提供信息,因为每个请求都会重置超时?不知道如何在不发出请求的情况下显示此内容?

无论如何,最好的方法是在每个请求上设置一些带有上次访问时间的会话变量。那应该提供远程信息。

You can get the timeout in minutes from:

Session.Timeout

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文