2013-05-14 46 views
2

我可以在Global.asax中處理表單身份驗證超時嗎?就像global.asax中的Session_End一樣?請指教。在ASP.net中處理表單身份驗證超時

我在我的webconfig這些設置超時在窗體身份驗證:

<forms name="formName" loginUrl="Login.aspx" protection="All" path="/" timeout="30"/> 

感謝所有! :)

回答

2

不,您不能因爲超時編碼在身份驗證Cookie上,並且存在於瀏覽器(不在服務器端)。

您可以作出這樣的習俗,也保持數據庫的用戶等待時間 - 但它不是那麼容易,替代你可以使用Application_AuthenticateRequest在Global.asax的請求之前,有權檢查,如果用戶沒有通過驗證任何更多。

如果用戶未通過身份驗證,如何刪除會話數據的一個示例。關於全球asax。

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    // get the authCookie 
    HttpCookie authCookie = Context.Request.Cookies[cookieName]; 
    // if is null then the use is not Authendicated 
    if (null == authCookie && System.Web.HttpContext.Current.Session != null) 
    { 
     // now check if you have Session variables that you wish to remove. 
     if(System.Web.HttpContext.Current.Session["flag"] == "1") 
     { 
      // remove your session data 


     } 
    } 
} 

您也許還要檢查與

if(HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated) 
{ 
     // now check if you have Session variables that you wish to remove. 
     if(Session["flag"] == "1") 
     { 
      // remove your session data   

     }  
} 
+0

我的問題是,當我的窗體身份驗證超時,會話變量仍然存在。如何處理這種情況,請告知,謝謝Aristos! – jomsk1e 2013-05-14 05:39:27

+0

@JRC我會在答案中加入一個例子。 – Aristos 2013-05-14 05:40:50

+0

你的第一個答案後,我試圖刪除global.asax中的Application_AuthenticateRequest中的會話,但我得到了一個HttpException,表示會話狀態在此上下文中不可訪問。 :( – jomsk1e 2013-05-14 05:56:57