3

I need to get the value of a cookie and update it. I must be doing something wrong because my cookie does not get updated. This is my code:

if (HttpContext.Current.Request.Cookies.AllKeys.Contains(EconnectConstants.FILE_SHARE_DOCUMENTS))
{
  var existingCookie = HttpContext.Current.Request.Cookies[EconnectConstants.FILE_SHARE_DOCUMENTS];
  existingCookie.Value = encriptedInput;
  existingCookie.Expires = DateTime.Now.AddMonths(1);
  HttpContext.Current.Response.Cookies.Set(existingCookie);
}
else
{
  var cookie = new HttpCookie(EconnectConstants.FILE_SHARE_DOCUMENTS, encriptedInput);
  cookie.Expires = DateTime.Now.AddMonths(1);
  cookie.Value = encriptedInput;
  HttpContext.Current.Response.Cookies.Add(cookie);
}  

Can anyone please tell me what I am doing wrong?

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
aleczandru
  • 5,319
  • 15
  • 62
  • 112
  • Make sure you're not trying to write a cookie on a 302 redirect. http://stackoverflow.com/questions/1621499/why-cant-i-set-a-cookie-and-redirect – Jamey Oct 09 '13 at 18:05
  • It is not the case this code executes after a user uploads a document – aleczandru Oct 09 '13 at 18:10
  • You check the request and set the request inthe first block, you should probably check the request and set the response. Request is what the server gets and Response is what the client gets – Silvermind Oct 09 '13 at 18:21
  • I just saw that mistake and fixed it but it still did not solve my problem – aleczandru Oct 09 '13 at 18:24

2 Answers2

2

This question is very old, but I faced this issue a time ago. As @argaz explain, since you are updating a value in the Response, you cannot get it from another object (the Request). Values are transferred from Request to Response in the next postback.

So, if your cookie stores data only valid for the current session AND you cannot wait to a postback happens, then instead implement a fake (as reading cookie values from same just updated Response according to special condition or force a postback to same page), consider the use Session object, which is not dependent of postbacks.

Fer R
  • 141
  • 2
  • 9
1

This seems suspicious:

HttpContext.Current.Request.Cookies.Set(existingCookie);

You are changing a property of the request object, it should not affect the response (which affects what is stored at the user).


You can probably remove the if and do:

HttpContext.Current.Request.Cookies[EconnectConstants.FILE_SHARE_DOCUMENTS].Value = encriptedInput;
HttpContext.Current.Request.Cookies[EconnectConstants.FILE_SHARE_DOCUMENTS].Expires = DateTime.Now.AddMonths(1);
argaz
  • 1,458
  • 10
  • 15
  • The Cookie property has not setter and I have also tryed chaing to this HttpContext.Current.Response.Cookies.Set(existingCookie); and it had no efect – aleczandru Oct 09 '13 at 18:19
  • fixed the setter issue. if nothing works try to view the traffic with fiddler, also set other cookies, maybe something is changing this cookie later in the request. – argaz Oct 09 '13 at 18:44