【发布时间】:2017-02-08 13:26:02
【问题描述】:
我正在尝试在 MVC 表单身份验证中处理失败的 ajax 请求。我在 Global.asax 中添加了以下代码。
protected void Application_BeginRequest()
{
HttpRequestBase request = new HttpRequestWrapper(Context.Request);
if (request.IsAjaxRequest())
{
Context.Response.SuppressFormsAuthenticationRedirect = true;
}
}
在主布局上,我添加了以下代码
$(document).ajaxError(function (event, request, settings) {
if (request.status == 401) {
window.location.href = '@Url.Action( "Login", "Account" )';
}
});
问题 1: 在正常调用中,表单身份验证会重定向到登录页面以及返回 url,这将帮助用户返回到他在到期前上次访问的同一页面。 http://localhost:53733/Account/Login?ReturnUrl=%2fUser%2fUserList
自从我使用 jQuery 重定向后,在 ajax 调用期间,它是 http://localhost:53733/Account/Login。如何确保即使在 ajax 调用中它也会与返回 url 一起重定向到登录页面
问题 2: 本地 ajax 函数错误处理程序也会在全局 ajax 错误处理程序之前触发。这会导致在导航到登录页面之前在页面上显示错误。不知道如何处理。请指教。
【问题讨论】:
标签: jquery ajax asp.net-mvc authentication form-authentication