Cookie not updating

Viewed 9208

I am trying to update a cookie value, but it doesn't work - everything I have tried doesn't update the cookie and I always get the initial value of the cookie.

So I have searched and according to MSDN I should be able to update a cookie by doing the following:

HttpCookie cookie = new HttpCookie("cookiename");
cookie.Value = cookieValue;
cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Set(cookie); // also just tried using .Add again here

As this didn't work, I did another search and people on SO said I should be able to do this:

HttpContext.Current.Response.Cookies["cookiename"].Value = cookieValue;
HttpContext.Current.Response.Cookies["cookiename"].Expires = DateTime.Now.AddDays(30);

But this didn't work either so I tried deleting the cookie and re-adding it:

HttpCookie cookie = new HttpCookie("cookiename");
cookie.Value = cookieValue;
cookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(cookie);

cookie.Expires = DateTime.Now.AddDays(30);
HttpContext.Current.Response.Cookies.Add(cookie);

I have also tried removing the cookie with the following before re-adding it

ResponseCookies.Remove("cookiename");

And this also didn't work, so I'm now not sure what else to try. Does anyone know how to update a cookie with c#?

Update

If I step through the code and check HttpContext.Current.Request.Cookies["cookiename"].Value after I have updated it, it shows the new value. If I then recycle the app pool so that the cookie has to be read from the file again, it shows the original value, so it seems the above code is not updating the physical cookie

2 Answers

I face a similar kind of issues. If fix this by using the following method to set and get the cookies.

 public static void SetCookie(string key, string value, TimeSpan expires, bool http = false)
        {
        
            if (HttpContext.Current.Request.Cookies[key] != null)
            {
                
                var cookieOld = new HttpCookie(key);
                cookieOld.Expires = DateTime.Now.Add(expires);
                cookieOld.Value = value;
                HttpContext.Current.Response.Cookies.Add(cookieOld);
            }
            else
            {
                HttpCookie encodedCookie = new HttpCookie(key, value);
                encodedCookie.Expires = DateTime.Now.Add(expires);
                HttpContext.Current.Response.Cookies.Add(encodedCookie);
            }
        }

And the following code used to GetCookie

 public static string GetCookie(string key)
        {
            string value = string.Empty;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];

            if (cookie != null)
            {
                value = cookie.Value;
            }
            return value;
        }

The following way to call get and set functions

public static string CustomerName
        {
            get { return CookieStore.GetCookie("customername"); }
            set { CookieStore.SetCookie("customername", value.ToString(), TimeSpan.FromHours(24), true); }
        }
Related