1

I have a multi key-value pair cookie. The cookie is a session cookie, i.e. not persistent. It contains several key-value pairs of data and well within the 4k size limit.

What's working: In my initialise action method (on Controller A), the cookie has a number of key-value pairs successfully added to it. All good so far.

Problem: When I navigate to another page, i.e. different action method (on Controller B), and then add a value to the cookie, I find all the previous values in the Cookie are now gone.

Please note, I verified the problem occurs only when a new value is added here. NOT adding a value to the cookie and navigating across many pages (and controllers) the existing values in the cookie are all preserved.

Investigations: I have spent one and a half days on this and tried a number of thing. In my cookie WriteCookie() method:

  1. Making the Cookie persistent (by setting it's expiry to tomorrow). Verified the cookie in Firefox exists and has the correct date. But once I add a value to the cookie in Controller B, the cookie has lost all it's data.

  2. Create a new cookie with the same name, added the previous values to this new cookie along with the new value. But again, navigating to another page (Controller B) and adding a value to the cookie has lost all it's previous data.

Code: Below is the original code I started with which doesn't contain the many heartbreaking attempts:

public static class CookieHelper
{
    public static string ReadCookie(string key)
    {
        string value = string.Empty;

        HttpCookie cookie = HttpContext.Current.Request.Cookies["mycookie"];
        if (cookie != null)
        {
            value = HttpContext.Current.Request.Cookies["mycookie"].Values[key];
        }

        return value;
    }

    public static void WriteCookie(string key, string value)
    {
        HttpContext.Current.Response.Cookies["mycookie"].Values[key] = value;
    }
}

Can anyone please help and explain why the cookie values are being lost. My guess, for some unknown reason subsequent writes to the cookie AFTER a HTTP Request is creating a new cookie and overwriting the existing cookie.

OpcodePete
  • 887
  • 1
  • 14
  • 28

1 Answers1

1

From MSDN:

You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client.

Modifying an individual subkey is the same as creating it.

To delete an individual subkey, you manipulate the cookie's Values collection, which holds the subkeys. You first recreate the cookie by getting it from the Cookies object. You can then call the Remove method of the Values collection, passing to the Remove method the name of the subkey to delete. You then add the cookie to the Cookies collection so it will be sent in its modified form back to the browser.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I read many different articles, SO posts, other posts, and even read *this* two or three times. But took a closer look at the section you highlighted, implemented Microsoft's snippet of code, and it WORKS!!! Thanks! The gotcha for me was you can modify the cookie (i.e. add values) many times, but once you send the cookie to the client (via a HttpResponse), you can't modify the cookie, you have to get the cookie, modify it (i.e. add, remove a value), the send it back to the client! Phew! – OpcodePete Aug 11 '15 at 22:47
  • Also, check out [this answer](http://stackoverflow.com/questions/5517273/httpcookiecollection-add-vs-httpcookiecollection-set-does-the-request-cookies#5517505). When updating a cookie, you can either allow duplicates to be created (using `Add`) or not allow them (using `Set`). – NightOwl888 Aug 12 '15 at 06:08