2010-06-16 73 views
-1

嗨我有一些ASP.NET MVC會話狀態的問題,在我實現下面的代碼段並將屬性放在方法上之後沒有過期。ASP.NET MVC中的會話過期問題

public sealed class SessionActionFilterAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     HttpContext ctx = HttpContext.Current; 
     //Check if session is supported 
     if (ctx.Session != null) 
     { 
      //Check if the session is new 
      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 = filterContext.HttpContext.Request.Headers["Cookie"]; 
       if ((sessionCookie != null) && (sessionCookie.IndexOf("ASP.NET_SessionId", StringComparison.OrdinalIgnoreCase) >= 0)) 
       { 
        //Redirect to the login page 
        ctx.Response.Redirect("~/Home/Index", true); 
        ctx.Response.End(); 
       } 
      } 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 

問題是重定向請求未執行,並且執行了具有SessionActionFilter屬性的Action。此方法使用已過期並導致錯誤的會話變量。

有人可以告訴我缺少什麼嗎?

非常感謝!

+1

我很好奇你使用會話的是什麼? – 2010-06-16 06:47:42

回答

0

我們正在存儲一些在我們的視圖中使用的數據!還有一個更新,我能夠運行這段現在工作正常的代碼。然而,我對使用cookie有點懷疑,並且需要將此代碼轉換爲無Cookie會話。這怎麼可能?