C# Cookies (problem with modifying/replacing)
I cannot modify last value of SessionKey which stored in cookies from server side. SessionKey in next request is still has old value. What wrong in my server's side code?
var varHttpListenerContextResponseCookie_SessionKey =
refHttpListenerContext.Response.Cookies[Constants.Cookies.LongNames.SessionKey];
if (varHttpListenerContextResponseCookie_SessionKey != null)
{
varHttpListenerContextResponseCook开发者_C百科ie_SessionKey.Value = refSessionKey;
}
else
{
refHttpListenerContext.Response.AppendCookie(
new System.Net.Cookie(Constants.Cookies.LongNames.SessionKey, refSessionKey));
}
Please help me!:)
You must remember to add your modified cookie to Response if you want to update value
// 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
From my understanding you want to modify your session Key. If it is right then you can make use of SessionManager, which will let you create a new session key. If this is not what you wanted please give more details about your question.
Thanks, Shashi
If your question is answered kindly mark it as Answered.
Also when modifying/adding a cookie, don't forget to create/increase the expiration, as expired cookies may not be retrievable
cookieForPage.Expires = DateTime.Today.AddYears(100);
精彩评论