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

查看:114
本文介绍了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错误:对象不是创建为对象的实例,没有找到Session变量,等等都是因为由于异步方法调用而未被处理的过期会话。

我如何处理这个行为,哪个是最好的方法来处理它(尽可能尝试不修改代码的很多部分应用程序)?

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 错误页面将显示为同步请求引起的错误页面。我发布了一个 simliar主题关于该主题。

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.

另一种方法是延迟会话超时。使用 setInterval() 每次会话到期 - 零十秒钟左右,向服务器发送一个轮询请求(这反过来基本上从请求中抓取会话)。

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天全站免登陆