Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookie value?

I'm doing the following to set a cookie value:

HttpCookie mycookie = new HttpCookie("mycookie"); mycookie.Value = "value1";  // Case sensitivity mycookie.Expires = DateTime.Now.Add(1); HttpContext.Current.Response.Cookies.Add(mycookie); 

Sometime later, I check the cookie using:

HttpCookie mycookie = HttpContext.Current.Request.Cookies["mycookie"]; 

I notice it still has an older value:

mycookie.Value == "oldValue" 

I can even check the cookie immediately after setting it and the value I've set isn't there. It's still the old value.

What is happening that the value isn't being set and how can I set it???

like image 334
4thSpace Avatar asked Nov 05 '15 22:11

4thSpace


1 Answers

Try this, you need to remove it and then add it

var response = HttpContext.Current.Response; response.Cookies.Remove("mycookie"); response.Cookies.Add(cookie); 
like image 141
eric_eri Avatar answered Sep 30 '22 21:09

eric_eri