我在使用 mvc3 razor 构建的网站中有许多 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 );
}