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

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

问题描述

我怎么能处理会话到期有JQuery的阿贾克斯方法的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变量没有发现,等一切都因为,这不是因为asynchronic方法调用处理的过期会话

我怎么能处理这种行为,这是处理它的最佳方法(试图尽可能不修改应用程序的code这么多地方)?

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的

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 错误页面将会显示。我已经发布了<一个href=\"http://stackoverflow.com/questions/1738785/handling-of-server-side-http-4nn-5nn-errors-in-jquerys-ajax-requests\">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.

另一种方法是ajaxically推迟会话超时。使用 的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天全站免登陆