MVC 与 JQuery:处理会话过期 [英] MVC with JQuery: handling Session Expire

查看:25
本文介绍了MVC 与 JQuery:处理会话过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理在某些页面上具有 JQuery Ajax 方法调用的 MVC 应用程序上的会话过期.问题如下:

How could I handle the session expire on a MVC application that has JQuery Ajax method calls on certain pages. The issue is the following:

  • 当客户端达到会话超时时,我的每个控制器都会继承一个类来检查会话是否处于活动状态(查找站点会话、数据库会话等某些内容)并将客户端重定向到一个新页面说会话到期;但是当我使用 JQuery ajax 在某些按钮点击时调用控制器的方法时情况有所不同,因为它跳过了继承类的验证并允许我留在页面上,但是当控制器尝试结束执行时方法,显然它会抛出 .Net 错误:对象不是作为对象的实例创建的,找不到会话变量等等.所有这些都是因为由于异步方法调用而没有处理的过期会话.

我该如何处理这种行为,哪种是最好的处理方式(尽可能不修改应用程序代码的那么多部分)?

How could I handle this behaviour, and which is the best way to handle it (trying as much as possible to not modify so much parts of the code of the application)?

提前致谢.

PD:告诉我我正在使用 Jquery 的 $.post() 可能很有用.

PD: It might be useful to tell that I'm using $.post() from Jquery.

推荐答案

这通常是不可恢复的错误,通常最好显示为独立的错误页面.配置 Web 应用程序以显示自定义的 4nn/5nn 错误页面并按如下方式设置 jQuery:

This is typically an unrecoverable error which is usually best to be displayed as a standalone error page. Configure the webapplication to display a customized 4nn/5nn error page and setup jQuery as follows:

$(document).ready(function() {
    $.ajaxSetup({
        error: handleXhrError
    });
});

function handleXhrError(xhr) {
    document.open();
    document.write(xhr.responseText);
    document.close();
}

这样服务器端 4nn/5nn 错误页面将被显示为好像它是由同步请求引起的.我发布了一个类似主题 关于之前的主题.

This way the server-side 4nn/5nn error page will be displayed as if it's been caused by a synchronous request. I've posted a simliar topic about the subject before.

另一种方法是ajaxically推迟会话超时.使用 setInterval() 向服务器(它基本上从请求中获取会话)每个 time-of-session-expiration-minus-10-seconds 左右.

Another way is to postpone the session timeout ajaxically. Use setInterval() to fire a poll request to the server (which in turn basically grabs the session from the request) every time-of-session-expiration-minus-ten-seconds or so.

setInterval(function() { $.get('poll'); }, intervalInMillis);

但需要注意的是,只要客户端打开浏览器窗口,会话就会继续存在,这有时可能需要太长时间.如有必要,您可以将其与某种活动检查器 结合使用,以便最大限度地减少会话过期的机会.您仍然需要将它与一个不错的 4nn/5nn 错误处理程序结合起来.

But the caveat is that the session will survive as long as the client has its browser window open, which may at times take too long. You can if necessary combine this with sort of activity checker so that the chance that the session get expired will be minimized. You still need to combine this with a decent 4nn/5nn error handler.

$(document).ready(function() {
    $.active = false;
    $('body').bind('click keypress', function() { $.active = true; });
    checkActivity(1800000, 60000, 0); // timeout = 30 minutes, interval = 1 minute.
});

function checkActivity(timeout, interval, elapsed) {
    if ($.active) {
        elapsed = 0;
        $.active = false;
        $.get('poll'); // Let server code basically do a "get session from request".
    }
    if (elapsed < timeout) {
        elapsed += interval;
        setTimeout(function() {
            checkActivity(timeout, interval, elapsed);
        }, interval);
    } else {
        alert($.messages.timeout); // "You will be logged out" blah blah.
        window.location = 'http://example.com/logout';
    }
}

这篇关于MVC 与 JQuery:处理会话过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆