2015-11-20 47 views
0

刪除這是我刪除所有cookie的方法:Cookie不能在註銷

private void ClearSessionAndCookies() 
{ 
    HttpContext.Current.Session.Remove(ExpirySession); 

    foreach (string cookieName in HttpContext.Current.Request.Cookies.AllKeys) 
    { 
     var cookie = new HttpCookie(cookieName) 
     { 
      HttpOnly = true, 
      Secure = Convert.ToInt32(ConfigurationManager.AppSettings["Https"]) == 1, 
      Domain = HttpContext.Current.Request.Cookies[cookieName].Domain, 
      Expires = DateTime.Now.AddDays(-1D) 
     }; 
     HttpContext.Current.Response.SetCookie(cookie); 
    } 

    HttpContext.Current.Session.Abandon(); 
} 

它完美其中在IIS中的所有網站在不同端口本地主機我開發的計算機上。但是,在QA環境中,每個網站都有不同的子域(qa.example.com,mz.example.com,cart.example.com),並且不會刪除Cookie。

我試着忽略cookie.Domain設置的行,沒有任何改變;萬一使用Cookies.Add和Cookies.Set試圖

另外,我用這段代碼在我傳統的ASP網站(mz.example.com),也完美地工作在我的電腦上:

For Each cookie in Response.Cookies 
    Response.Cookies(cookie).Expires = DateAdd("d",-1,now()) 
Next 

在QA沒有,但舊代碼工作

Response.AddHeader "Set-Cookie", cookie & "=; Expires = Thu, 01-Jan-70 00:00:01 GMT; Path=/; HttpOnly; " & "Domain =" & Session("Domain") 

我缺少相關的不同的子域的東西嗎?所有網站中的域都設置爲「.example.com」。

回答

0

我已經有這個註銷問題超過兩個星期了,我用了一些黑客來解決它(URL參數,重定向等)。它花費我近40個小時的調試,研究和測試。

在主站點的web.config文件中,缺少導致所有問題的一個cookie的域信息。

<authentication mode="Forms"> 
    <forms name="problemCookie" defaultUrl="http://qa.example.com/" enableCrossAppRedirects="true" loginUrl="login.aspx" protection="All" path="/" /> 
</authentication> 

加入domain=".example.com"解決了這個問題。