gpt4 book ai didi

jquery - 当 FormsLogin session 不再事件时,ASP.NET MVC 强制将 AJAX 请求重定向到登录页面

转载 作者:行者123 更新时间:2023-12-03 21:57:40 25 4
gpt4 key购买 nike

我有一些 AJAX 调用,通过 jQuery.AJAX 方法呈现 PartialViewResults。这非常有效,我的 View 完全按照我想要的方式呈现。

当我将页面保留一段时间并且表单例份验证 session 过期时,就会出现问题。当我单击执行 AJAX 请求的操作时,它会在我的 div 中显示登录页面。

我希望它将整个页面重定向到登录页面。

最佳答案

在 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
});

关于jquery - 当 FormsLogin session 不再事件时,ASP.NET MVC 强制将 AJAX 请求重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3431343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com