2010-12-08 98 views
6

我正在使用ASP身份驗證和集成的webservice。ASP/ASPXAUTH身份驗證Cookie未在符號/登出時清除

用戶使用登錄頁面上的Forms身份驗證登錄。
要註銷,我從Silverlight調用身份驗證Web服務並調用註銷。

一切工作正常,但現在有時IE變得瘋狂,不再註銷用戶。

我使用了Fiddler,事實證明,身份驗證服務返回一個SetCookie來清除ASPXAUTH cookie,但在下一次調用時IE仍然有cookie集。
因此,當然,因爲cookie在那裏,用戶被認證並且重新登錄,而不是被定向到登錄頁面。

我查過並沒有看到任何其他問題的描述。 我不能複製它,我的同事有一個行爲不當的IE瀏覽器在一個環境下工作正常,而另一個(一個對DEV有問題,另一個對PreProd服務器有問題)。

任何想法可能會發生什麼?

+0

一個常見問題是Cookie被設置在一個上下文中並試圖從另一個上下文中刪除。例如。確保刪除Set-Cookie標頭上的路徑和域屬性與原始Set-Cookie調用中的路徑和域屬性完全匹配。 – EricLaw 2010-12-08 14:23:11

+0

不幸的是,正如我所說的,我無法控制這一點。我調用FormsAuthentication.RedirectFromLogin來設置cookie並調用Web服務LogOut方法。 – R4cOON 2010-12-08 15:22:31

回答

2

爲了避免這個問題,您在創建SignOut的那一刻,下一次調用必須與重定向(pageLogOut,true);並停止任何其他活動,直到完全重定向。參數true是非常重要的。

在您調用SignOut()之後,您必須強制瀏覽器刷新cookie數據,因爲如果認證由於某種原因再次請求cookie,則cookie會有更多時間存活並且不會從瀏覽器中將其刪除你用SigntOut命令請求。

因此,在SignOut之後,重定向到一個頁面 - 或者確保您將cookies刷新到瀏覽器,而不是再次詢問任何與用戶身份驗證有關的事情,直到cookies完全記錄到瀏覽器。

希望得到這個幫助。

+0

MSDN建議使用false重定向,然後調用CompleteRequest以防止ThreadAbortException。他們也提到性能,但我沒有測試過。從[MSDN - 響應重定向](http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx):如果您爲endResponse參數指定了true,則此方法會爲原始請求調用End方法,當它完成時拋出一個ThreadAbortException異常。此異常對Web應用程序性能有不利影響,因此建議爲endResponse參數傳遞false。 – 2013-02-25 13:37:06

4

我有這個問題,並作出肯定的是,用戶被註銷,現在我用下面的代碼:

 FormsAuthentication.SignOut(); 

     // Drop all the information held in the session 
     Session.Clear(); 
     Session.Abandon(); 

     // clear authentication cookie 
     HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, ""); 
     cookie1.Expires = DateTime.Now.AddYears(-1); 
     Response.Cookies.Add(cookie1); 

     // clear session cookie 
     HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", ""); 
     cookie2.Expires = DateTime.Now.AddYears(-1); 
     Response.Cookies.Add(cookie2); 

     // Redirect the user to the login page 
     Response.Redirect("YourLoginPage.aspx", true); 
0

很可能你的問題的經驗是在問候Cookie域。該cookie可能寫入"." + FormsAuthentication.CookieDomain。我之前已將Cookie設置爲「admin.example.com」域,並且已看到以.爲前綴的cookie。在開發環境中,它被寫入localhost

我使用的解決方案是爲每個驗證cookie和會話cookie添加兩個cookie。

所以我使用的解決方案如下:

protected void SignOut(HttpContext Context) 
    { 
     FormsAuthentication.SignOut(); 
     Context.Session.Abandon(); 

     // clear authentication cookie 
     Context.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName) 
     { 
      Path = FormsAuthentication.FormsCookiePath, 
      Value = "", 
      Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? Convert.ToString(FormsAuthentication.CookieDomain) : Context.Request.Url.Host, 
      HttpOnly = true, 
      Expires = DateTime.Now.AddYears(-1) 
     }); 

     Context.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName) 
     { 
      Path = FormsAuthentication.FormsCookiePath, 
      Value = "", 
      Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? "." + Convert.ToString(FormsAuthentication.CookieDomain) : "." + Context.Request.Url.Host, 
      HttpOnly = true, 
      Expires = DateTime.Now.AddYears(-1) 
     }); 

     // clear session cookie (not necessary for the current problem but recommended anyway) 

     Context.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") 
     { 
      Path = FormsAuthentication.FormsCookiePath, 
      Value = "", 
      Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? Convert.ToString(FormsAuthentication.CookieDomain) : Context.Request.Url.Host, 
      HttpOnly = true, 
      Expires = DateTime.Now.AddYears(-1) 
     }); 


     Context.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") 
     { 
      Path = FormsAuthentication.FormsCookiePath, 
      Value = "", 
      Domain = (Convert.ToString(FormsAuthentication.CookieDomain).Length > 0) ? "." + Convert.ToString(FormsAuthentication.CookieDomain) : "." + Context.Request.Url.Host, 
      HttpOnly = true, 
      Expires = DateTime.Now.AddYears(-1) 
     }); 


     FormsAuthentication.RedirectToLoginPage(); 
    } 

這個調用將添加以下標題到響應

位置的結果:/Login.aspx? RETURNURL =默認。aspx

Set-Cookie:**** =;到期=星期二,1999年10月12日05:00:00 GMT;路徑= /; HttpOnly

Set-Cookie:**** =;域= admin.example.com;到期= 2014年4月23日星期三18:04:58 GMT;路徑= /; HttpOnly

Set-Cookie:**** =;域= .admin.example.com;到期= 2014年4月23日星期三18:04:58 GMT;路徑= /; HttpOnly

Set-Cookie:ASP.NET_SessionId =; domain = admin.example.com expires = Wed,23-Apr-2014 18:04:58 GMT;路徑= /; HttpOnly

Set-Cookie:ASP.NET_SessionId =; domain = .admin.example.com expires = Wed,23-Apr-2014 18:04:58 GMT;路徑= /;僅Http

哪裏***是包含我的加密身份驗證票值我的cookie的名稱;


注意,第一Set-Cookie很可能從FormsAuthentication.SignOut()方法調用生成。