Asp.Net MVC 2 中会话超时时的确认

发布于 2024-10-06 13:25:50 字数 179 浏览 3 评论 0原文

我想在会话超时还剩 2 分钟时提示用户。如果用户按,它将刷新当前会话,而不会丢失表单数据;如果用户选择,则会注销其会话。我想知道如何捕获剩余的两分钟以及如何刷新当前会话而不丢失表单数据。我使用 Asp.Net MVC 2 和 JQuery,还有一件事我没有使用表单身份验证

I want to prompt the user when 2minutes will left in session timeout. If user press Yes it refreshes its current session without losing the form data if user select No then it logout its session. I want to know how to capture that two minutes are remaining and how to refresh the current session without losing form data. Im using Asp.Net MVC 2 with JQuery, One more thing Im not using Form Authentication

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

枫以 2024-10-13 13:25:50

好吧,每次请求后都会刷新会话。您可以在 Session.Timeout 上找到会话超时。您可以在母版页中执行以下操作:

<script type="text/javascript">
function keepAlive() {
    window.clearTimeout(window.sessionKeepAlive);
    window.sessionKeepAlive = window.setTimeout(function() {

        if(confirm('refresh session?')) {
            // submit ajax request
        } else {
            // logout
        }

    }, <%= (Session.Timeout - 2) * 60 * 1000 %>);
}

keepAlive();
</script>

然后,如果您使用 AJAX,则需要在每次 AJAX 完成回调时重置超时:

$(document).ajaxComplete(keepAlive);

很难获得 100% 的可靠性。您需要确保确认回调中的 AJAX 请求实际触及状态服务器,并且需要处理 AJAX 请求失败时发生的情况。即使您有此脚​​本,这些事情也可能导致您的会话过期。

硬币的另一面是这种情况会比必要时更频繁地出现。例如,如果您通过 javascript 将图像添加到 DOM,并且该图像的检索正在触及您的状态,则该请求不会重置 javascript 超时,尽管会话已刷新。

Well, the session is refreshed after each request. You can find the session timeout on Session.Timeout. You could do something like this in your masterpage:

<script type="text/javascript">
function keepAlive() {
    window.clearTimeout(window.sessionKeepAlive);
    window.sessionKeepAlive = window.setTimeout(function() {

        if(confirm('refresh session?')) {
            // submit ajax request
        } else {
            // logout
        }

    }, <%= (Session.Timeout - 2) * 60 * 1000 %>);
}

keepAlive();
</script>

Then, you need to reset the timeout at every AJAX complete callback, if you're using AJAX:

$(document).ajaxComplete(keepAlive);

It'll be difficult to get this 100% reliable. You need to make sure that the AJAX request in the confirm callback actually touches the state server, and you need to handle what happens when that AJAX request fails. Those are the things that could cause your session to expire even though you have this script.

The other side of the coin is where this will pop up more often than necessary. For instance, if you're adding an image to the DOM through javascript, and the retrieval of that image is touching your state, that request will not reset the javascript timeout, although the session has been refreshed.

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