没有重定向到登录页面时,会话超时与ajax调用
问题描述:
我在我的网站有很多ajax调用是使用mvc3剃须刀构建。 问题是,如果在使用ajax进行操作时会话超时后,它不会重定向到登录页面。 我正在使用如下所示的属性来处理会话超时。没有重定向到登录页面时,会话超时与ajax调用
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext ctx = HttpContext.Current;
// check if session is supported
if(ctx.Request.IsAuthenticated)
{
if (ctx.Session != null)
{
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (null != sessionCookie)
{
FormsAuthentication.SignOut();
const string loginUrl = @"~/Login/Login";
var rr = new RedirectResult(loginUrl);
filterContext.Result = rr;
}
}
}
}
else
{
ctx.Response.Redirect(@"~/Login/Login");
}
base.OnActionExecuting (filterContext);
}
答
你传递的结果是发送一个html页面和一个302到ajax请求作为响应。还有其他方法可以解决这个问题:
- 给定制的响应代码,并在ajax请求中检查它,并检查成功函数中的响应代码。
- 或者发送一个JSON对象包含响应对象,并检查它在那里
这可以在ajaxComplete事件这两种方法来实现,并可能被添加到母版页/布局或哪里有阿贾克斯电话。
由于存在许多其他问题,因此不会粘贴到代码中。 欢迎来到* Vaibhav!
在问你自己之前搜索与你有关的问题。大多数时候你会得到它们。 – bhuvin 2012-08-01 08:10:51