33

I have very large website which uses a lot of cookies. There are approx. 14 different cookies are there. I have different cookies for each item. When a user surfs the site they will have 14 cookies in their browser. I do not want this.

I want a single cookie for my site that will have 14 items and I can add,edit and delete them. I tried many ways but I am not able to do this.

I need to put some run time cookies as well save the user name in cookie. After the user logs in I want to save their personal site address in it. Eventually I want both the user name and personal site address both. I want to save user name before and then when user goes to his personal site then i will store personal site name run time.

Does any one have an idea how I could do this?

Jalpesh Vadgama
  • 13,653
  • 19
  • 72
  • 94

3 Answers3

53

Matthew beat me to it, but yes, see the ASP.NET Cookies Overview...

To write and read a single cookie with multiple key/values, it would look something like this:

HttpCookie cookie = new HttpCookie("mybigcookie");
cookie.Values.Add("name", name);
cookie.Values.Add("address", address);

//get the values out
string name = Request.Cookies["mybigcookie"]["name"];
string address = Request.Cookies["mybigcookie"]["address"];
Kurt Schindler
  • 21,037
  • 4
  • 43
  • 48
  • still you are storing at same time.name and address like i did following HttpCookie cookie = new HttpCookie("mybigcookie"); cookie.Values.Add("name", name); and then when user uses its personal site then i want to do cookie.Values.Add("address", address); Is it possible? – Jalpesh Vadgama Jul 23 '09 at 19:08
  • What's the javascript equivalent syntax? – JNappi Dec 12 '11 at 19:50
  • 2
    Javascript equivalent: document.cookie="mybigcookie=name=John&address=John's Address" – Eddie Groves Jun 12 '14 at 02:19
  • Actually to get the value out of a cookie it should be string name = Request.Cookies["mybigcookie"]["name"]; – Zath Oct 03 '14 at 15:04
  • removing the cookie does not remove 'name' key, why? – Pranav Bilurkar Mar 23 '17 at 09:59
17

There is a section in the ASP.NET Cookies Overview that discusses how to implement multiple name-value pairs (called subkeys) in a single cookie. I think this is what you mean.

The example from that page, in C#:

Response.Cookies["userInfo"]["userName"] = "patrick"; //userInfo is the cookie, userName is the subkey
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString(); //now lastVisit is the subkey
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);

EDIT: From the Cookies Overview (emphasis added):

Modifying and Deleting Cookies: 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.

Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • In that they are storing cookie at same time like username and personal site and then add it to cookie while in my case its some thing like i already stored user name in cookie and then i add one another values in cookie like personal site. Is there any ways to do this? – Jalpesh Vadgama Jul 23 '09 at 18:53
1

Modifying and Deleting Cookies: 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.