2012-05-19 42 views
4

我有具有環,其在每個循環結束時休眠6秒函數註銷asp.net MVC後User.Identity.IsAuthenticated真

Thread.Sleep(TimeSpan.FromSeconds(6)); 此循環10次,即功能60秒運行,每次暫停6秒鐘。

我有認證測試在循環

if (!HttpContext.Current.User.Identity.IsAuthenticated) 
       { 
        return null; 
       } 

的起點,從而在每次認證首創然後運行和等待6秒時間。

這是我的函數:

while (counter < 10) 

      { 
       if (!HttpContext.Current.User.Identity.IsAuthenticated) 
       { 
        return null; 
       } 

       // doing stuff 

       Thread.Sleep(TimeSpan.FromSeconds(6)); 
       counter++; 
      } 

現在用戶在此期間註銷(比如說在15秒)。我使用ajax註銷,因此不想重定向我的網頁。即使在註銷IsAuthenticated始終是所有10個循環真,是假的,只有當重新執行

該功能登出我使用:

FormsAuthentication.SignOut(); 
Session.Abandon(); 
Response.Cookies.Remove(FormsAuthentication.FormsCookieName); 
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); 
HttpCookie cookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName]; 
       if (cookie != null) 
       { 
        cookie.Expires = DateTime.Now.AddDays(-1); 
        Response.Cookies.Add(cookie); 
       } 

但仍其真正的.. 我想停止用戶註銷後立即執行我的線程

回答

5

發生這種情況是因爲IsAuthenticated有一個內部高速緩存,因爲一次又一次地進行此驗證的時間太長。因此,在你的循環內,離開頁面時,IsAuthenticated不會改變。

另一方面,這是什麼意思?在一個循環中,用戶可以看到前4個想法,然後看不到剩下的部分,因爲沒有更多的認證?沒有意義。

如果您想檢查用戶是否已經離開並離開了頁面,您可以做些什麼來檢查其他參數。

這是顯示此內部緩存的代碼。

public virtual bool IsAuthenticated 
{ 
    get 
    { 
     if (this.m_isAuthenticated == -1) 
     { 
      WindowsPrincipal principal = new WindowsPrincipal(this); 
      SecurityIdentifier sid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[] { 11 }); 
      this.m_isAuthenticated = principal.IsInRole(sid) ? 1 : 0; 
     } 
     return (this.m_isAuthenticated == 1); 
    } 
} 
+0

實際上它是一個長輪詢功能,得到了用戶的所有通知,現在因爲功能一分鐘運行和用戶之間不應該完成。用戶永遠不會離開它只是將註銷頁面的請求註銷。以及我可以檢查的其他參數是什麼 – deepakgates

+0

@deepakgates也許cookie上的值可以指示註銷並指示它停止。也許是數據庫表的一個值,並且該行與用戶登錄cookie關聯。它取決於你,快速。 – Aristos

+0

是否有可能從函數 – deepakgates