Storing multiple values in cookies

asp.netcookies

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?

Best Answer

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"];