2011-02-22 127 views
2

我的Cookie存在問題。我通過LDAP對用戶進行身份驗證,只要瀏覽器保持打開狀態,用戶就不必重新登錄到該工具。只要瀏覽器處於打開狀態,他們甚至可以關閉標籤。Cookie不會持續

但是,當用戶關閉瀏覽器時cookie會被刪除。我爲此搜索了很多谷歌,並且我無法獲得任何解決方案,例如this onethat one

這裏是我的設置,一旦我的登錄頁面上進行身份驗證:

String encryptedTicket = FormsAuthentication.Encrypt(authTicket); 

//Create a cookie, and then add the encrypted ticket to the cookie as data. 
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 

//Add expiration date to the cookie 
authCookie.Expires = DateTime.Now.AddMonths(1); 

//Add the cookie to the outgoing cookies collection. 
Response.Cookies.Add(authCookie); 

//You can redirect now. 
FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false); 

我的web.config看起來是這樣的:

<authentication mode="Forms"> <forms loginUrl="Logon.aspx" timeout="43200" name="adAuthCookie" path="/" slidingExpiration="true" /> </authentication>

無論我做什麼了ASP.NET_SessionId和adAuthCookie餅乾始終設置爲「當我關閉瀏覽器時」。 我想避免我的用戶在關閉瀏覽器時始終需要登錄,而是每月只做一次。

+0

你的'authTicket'看起來像什麼,你使用的是什麼會員供應商? – Nimrod 2011-02-23 00:49:36

+0

authTicket看起來很正常,CookiePath爲「/」,Expired爲false,IsPersistent爲true等等。我能看到的唯一奇怪的事情是Expiration日期設置爲1h後。至於會員供應商,我仍然是新手,但我不相信我在這種情況下使用了一個。 – LanFeusT 2011-02-23 01:24:37

回答

0

聽起來像你需要在你的web.config中設置machineKey。 如果沒有指定或者machine.config,那麼它將在啓動時生成並在每次重新啓動網站時重置,從而使cookie中的加密無效。

machineKey element

+0

恐怕沒有幫助。我補充說:'''''我也試着添加手動按鍵, – LanFeusT 2011-02-23 00:35:31

+0

我在這裏生成我的[http://aspnetresources.com/tools/machineKey](http://aspnetresources.com/tools/machineKey)確保你的格式正確。儘管如此,這仍然耗盡了我的想法。 – 2011-02-23 00:45:26

+0

謝謝,我嘗試過,但仍然沒有運氣。我向WebConfig的表單部分添加了'protection =「All」',但這並沒有什麼區別。 Cookie的過期日期仍然設置爲「瀏覽器關閉時」。 – LanFeusT 2011-02-23 01:15:23

0

確保瀏覽器未設置爲關閉時刪除Cookie。安全選項中有一些偏執的設置(我認爲在IE中它是高級 - >安全 - >空臨時文件夾...)

使用Fiddler或其他工具來確保您發送的Cookie正確到期到瀏覽器。這樣您可以驗證服務器或客戶端的錯誤。

+0

瀏覽器未設置爲關閉時刪除Cookie。至於期滿,我可以看到,實際上ASP.NET_SessionId具有正確的到期日期設置爲一個月後。但是,adAuthCookie沒有出於某種原因。 – LanFeusT 2011-02-23 00:55:40

+0

「adAuthCookie不過出於某種原因」聽起來像是你看到的行爲的解釋。很奇怪 - 在authCookie上設置cookie過期的代碼看起來沒問題。您確定這是添加此Cookie的唯一代碼嗎? – 2011-02-23 02:19:36

+0

對不起,我花了很長時間回覆。是的,這是影響此Cookie的唯一代碼。我仍然有這個問題:/ – LanFeusT 2011-03-01 23:07:16