如何使用ajax检查我的MVC会话是否过期?

时间:2022-04-29 01:28:28

I have my web.config set apropriately and it works fine but only when the user does a httpPost it recognizes that it should redirect:

我有适当的web.config设置,它工作正常,但只有当用户执行httpPost时,它才会识别它应该重定向:

  <system.web>
<sessionState mode="InProc" timeout="5" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
<authentication mode="Forms">
  <forms loginUrl="~/Login/Login"></forms>
</authentication>

I have this code set in my _Layout:

我在_Layout中设置了此代码:

var _redirectUrl = '@Url.Action("Logout", "Login")';
$(document).ajaxSuccess(function (event, request, settings, xhr, props, jqXHR) {
            if (jqXHR.status == 401) {
                window.location.href = _redirectUrl;
            }
        });

jqXHR.status returns undefined in my browser debuger, I also tried props.status and still get undefined. Any suggestions?

jqXHR.status在我的浏览器debuger中返回undefined,我也尝试过props.status并且仍然未定义。有什么建议?

1 个解决方案

#1


2  

A request to a timed-out session will result in a http status 302 (Found), that results in a redirect that is transparent. jQuery therefore does not handle this automatically. You could have the server return a http status 401 (Unauthorized) instead of the redirect. Here is an article with code you can copy to do that:

对超时会话的请求将导致http状态302(Found),这导致重定向是透明的。因此jQuery不会自动处理这个问题。您可以让服务器返回http状态401(未授权)而不是重定向。这是一篇包含您可以复制的代码的文章:

http://blogs.perficient.com/microsoft/2014/02/gracefully-handle-mvc-user-session-expiration-in-javascript/

#1


2  

A request to a timed-out session will result in a http status 302 (Found), that results in a redirect that is transparent. jQuery therefore does not handle this automatically. You could have the server return a http status 401 (Unauthorized) instead of the redirect. Here is an article with code you can copy to do that:

对超时会话的请求将导致http状态302(Found),这导致重定向是透明的。因此jQuery不会自动处理这个问题。您可以让服务器返回http状态401(未授权)而不是重定向。这是一篇包含您可以复制的代码的文章:

http://blogs.perficient.com/microsoft/2014/02/gracefully-handle-mvc-user-session-expiration-in-javascript/