views:

1835

answers:

3

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?

+2  A: 

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
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
A: 

try this in VB.NET:

Response.Cookies("info")("UserName") = "Jalpesh"
Response.Cookies("info")("PersonalAddress") = "address"

Or this in C#:

Response.Cookies["info"]["UserName"] = "Jalpesh";
Response.Cookies["info"]["PersonalAddress"] = "address";
CD
+2  A: 

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 = response.Cookies["mybigcookie"]["name"];
string address = response.Cookies["mybigcookie"]["address"];
Kurt Schindler
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
@Jalpesh: short answer is no, check my edit.
Matthew Jones