2011-08-24 25 views
0

我無法修改從服務器端存儲在Cookie中的SessionKey的最後一個值。 SessionKey在下一個請求中仍然具有舊值。我的服務器端代碼有什麼問題?C#Cookies(修改/替換問題)

var varHttpListenerContextResponseCookie_SessionKey = 
    refHttpListenerContext.Response.Cookies[Constants.Cookies.LongNames.SessionKey]; 

if (varHttpListenerContextResponseCookie_SessionKey != null) 
{ 
    varHttpListenerContextResponseCookie_SessionKey.Value = refSessionKey; 
} 
else 
{ 
    refHttpListenerContext.Response.AppendCookie(
     new System.Net.Cookie(Constants.Cookies.LongNames.SessionKey, refSessionKey)); 
} 

請幫幫我!:)

回答

3

你必須記住您的修改Cookie添加到響應,如果你想更新值

// get existing cookie or create new 
var cookie = Request.Cookies[Constants.Cookies.LongNames.SessionKey] ?? new HttpCookie(Constants.Cookies.LongNames.SessionKey); 
// set cookie value 
cookie.Value = refSessionKey; 
// add cookie to http repsonse 
Response.Cookies.Add(cookie); 

MSDN - Basics of Cookies in ASP.NET

0

從要修改你的會話密鑰我的理解。如果它是正確的,那麼你可以使用SessionManager,它可以讓你創建一個新的會話密鑰。如果這不是你想要的,請給出關於你的問題的更多細節。

感謝, 沙市

如果您的問題被回答好心將其標記爲已回答。

0

而且當修改/添加一個cookie,不要忘記創建/增加到期,因過期的cookies可能無法檢索

cookieForPage.Expires = DateTime.Today.AddYears(100);