私のウェブサイトでは、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 );
}