jQuery – ASP.NET MVC强制将AJAX请求重定向到登录页面时,FormsLogin会话不再活动

我有一些 AJAX调用,通过jQuery. AJAX方法渲染PartialViewResults.这样做很棒,我的意见让我完全按照我想要的方式呈现.

当我离开页面一段时间并且Forms auth会话到期时,会出现问题.当我点击执行AJAX请求的操作时,它会显示我的div中的登录页面.

我想要将WHOLE页面重定向登录页面.

解决方法

在Global.asax的Application_EndRequest()方法中设置它

您可以检查请求是否是ajax请求,并检查是否发送HTTP重定向(302),如果是,则我们会想要发送401.

protected void Application_EndRequest() {
            var context = new HttpContextwrapper(Context);
            // If we're an ajax request,and doing a 302,then we actually need to do a 401
            if (Context.Response.StatusCode == 302 && context.Request.IsAjaxRequest()) {
                Context.Response.Clear();
                Context.Response.StatusCode = 401;
            }
        }

然后在您的客户端代码中,在全球可访问的区域:

MyNamespace.handleAjaxError = function (XMLHttpRequest,textStatus,errorThrown) {
    if (XMLHttpRequest.status == 401) {
        // perform a redirect to the login page since we're no longer authorized
        window.location.replace("logout path");
    }    
    } else {
        MyNamespace.displayGeneralError();
    }
};

  $.ajax({
  type: "GET",url: userAdmin.addUserActionUrl,success: userAdmin.createuserEditorDialog,error: MyNamespace.handleAjaxError
 });

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...