Can't Update of Cookie

S

Seb

Hi All,

I'm a bit confused. I simply try to create a new Cookie. And Update my
cookie later when another call is made to my Page. The cookie creation
works properly, but the update does not work at all. I'm probably
missing something but I can't find what. Any help will be really
appreciated.

Here is the Code I execute to Create my Cookie:
HttpCookie httpCookie = new HttpCookie("MyCookie");
httpCookie.Expires = DateTime.Now.AddDays(30);
httpCookie.Path = "/";
httpCookie.Value = "MyInitValue";
Response.Cookies.Add(httpCookie);

If I call back the Page and check what is in
Request.Cookies["MyCookie"].Value it returns "MyInitValue".

Now I try to update the cookie here is the code:
Response.Cookies.Set(Request.Cookies["MyCookie"]);
Response.Cookies["NewBasket"].Value = "MyNewValue";
Response.Cookies["NewBasket"].Expires = DateTime.Now.AddDays(30);

After this code is executed when I check
Request.Cookies["MyCookie"].Value it returns always "MyInitValue".

Can anybody see what I'm missing ?

I will appreciate your help.

Kind regards.

Sébastien.
 
J

jpontani

Request.Cookies is used for accessing the value only, while Response.Cookies
is used for setting values (or updating, in your case).

You need to use Response.Cookies to update the cookie, and not
Request.Cookies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top