2012-03-10 120 views
0

我正在寫一個「記住我的用戶名」Cookie,並在自定義的持續時間內過期。一個月。我注意到,當我添加HttpOnly = true時,到期會變爲會話。爲什麼是這樣?我似乎無法找到任何有關這種情況發生的文件。HTTPOnly將cookie過期設置爲會話

謝謝。

+0

歡迎堆棧溢出。請記住,讚賞是通過upvotes和接受的答案來顯示的。我強烈建議所有新用戶仔細閱讀[faq],尤其是[ask] :) – 2012-03-10 19:41:09

+0

您能告訴我們您用來設置和閱讀cookie的代碼嗎? – LukeH 2012-03-10 22:47:14

回答

0

我添加以下代碼:還有,現在我就比一個不同的行爲標題。我在VS2010內置服務器上本地運行。它似乎表現出不一致的行爲。我會在Expires之前和之後移動HttpOnly = true,它似乎會改變行爲,直到我刷新瀏覽器頁面。所以,我假設一切都很好,從來沒有問題。另外,我將HttpOnly和Secure標誌移至web.config,因爲並非我所有的環境都具有SSL。


FormsAuthenticationTicket ticket = new FormsAuthenticationTicket 
               (strUserID, //name 
               false, //IsPersistent 
               24 * 60); // 24 hours 

// Encrypt the ticket. 
string encryTicket = FormsAuthentication.Encrypt(ticket); 

// Create the cookie. 
HttpCookie userCookie = new HttpCookie("Authentication", encryTicket); 
userCookie.HttpOnly = true; 
Response.Cookies.Add(userCookie); 

e.Authenticated = true; 
if (LoginPannelMain.RememberMeSet) 
{ 
    HttpCookie aCookie = new HttpCookie("email", strUserLogin); 
    aCookie.HttpOnly = true; 
    aCookie.Expires = DateTime.Now.AddYears(1); 
    Response.AppendCookie(aCookie); 
} 
else 
{ 
    HttpCookie aCookie = new HttpCookie("email", ""); 
    aCookie.HttpOnly = true; 
    Response.AppendCookie(aCookie); 
} 
+0

我想通了,我沒有選中複選框,在代碼中有過期代碼導致我原來的問題。 – evodev 2012-03-13 23:45:22

0

Here是文檔。

如果cookie具有HttpOnly屬性且無法通過客戶端腳本訪問 ,則爲true;否則,是錯誤的。默認值是false。

基本上,它變成一個會話變量,因爲它只會被存儲在服務器上,由於你的設置