Handling cookies in ASP .NET
How To Create a Cookie
Here’s a new cookie named books
HttpCookie myCookie = new HttpCookie(”books”);
We created the cookie but there are no keys with values in it, so for now it’s useless. So let’s add some:
myCookie.Values.Add(”b1″, “book1″);myCookie.Values.Add(”b2″, “book2″);
We also need to add the cookie to the cookie collection
Response.Cookies.Add(myCookie);
How to get the value stored in a cookie.Here’s how to get the keys and values stored in a cookie:
Here’s how to get the keys and values stored in a cookie:Response.Write(myCookie.Value.ToString());
The output to using this with the previous created cookie is this: “b1=book1&b2=book2″.
However, most of the time you’ll want to get the value stored at a specific key. If we want to find the value stored at our b2 key, we use this:
Response.Write(myCookie[”b2″].ToString());
Set the lifetime for a cookie.You can easily set the time when a cookie expires. We’ll set the Expires property of myCookie to the current time + 12 hours:
You can easily set the time when a cookie expires. We’ll set the property of to the current time + 12 hours:myCookie.Expires = DateTime.Now.AddHours(12);
This cookie will expire in twelve hours starting now. You could as well make it expire after a week:
myCookie.Expires = DateTime.Now.AddDays(7);
Also note that if you don’t set a cookie’s expiration date & time a transient cookie will be created - a cookie which only exists in the current browser instance. So if you want the cookie to be stored as a file you need to set this property.
Setting the cookie’s path.Sometimes you’ll want to set a path for a cookie so that it will be available only for that path in your website (ex.: www.abc.com/xyz). You can set a cookie’s path with the Path property:
Sometimes you’ll want to set a path for a cookie so that it will be available only for that path in your website (ex.: ). You can set a cookie’s path with the property:myCookie.Path = “/xyz”;
Setting the domain for a cookie.Perhaps instead of using http://www.abc.com/xyz path style to your xyz, you would use a subdomain like http://xyz.abc.com. The Domain property should do it:
Perhaps instead of using path style to your xyz, you would use a subdomain like . The property should do it:myCookie.Domain = “xyz.abc.com“;
How to edit a cookie.You don’t actually edit a cookie, you simply overwrite it by creating a new cookie with the same key(s).
You don’t actually edit a cookie, you simply overwrite it by creating a new cookie with the same key(s).How to destroy / delete a cookie.There’s no method called Delete which deletes the cookie you want. What you can do if you have to get rid of a cookie is to set its expiration date to a date that has already passed, for example a day earlier. This way the browser will destroy it.
There’s no method called which deletes the cookie you want. What you can do if you have to get rid of a cookie is to set its expiration date to a date that has already passed, for example a day earlier. This way the browser will destroy it. myCookie.Expires = DateTime.Now.AddDays(-1);
How to remove a subkey from a cookie.You can use the Remove method:
You can use the method:myCookie.Values.Remove(”b2″);
However, you don’t usually remove a subkey immediatly after creating it, so first we need to retrieve the cookie, remove the subkey and then add it back to the Cookies collection:
// Get the cookie from the collection (jar)
myCookie = Request.Cookies[”books”];
// Remove the key ‘b2′
myCookie.Values.Remove(”b2″);
// Add the cookie back to the collection (jar)
Response.Cookies.Add(myCookie);
// See what’s in the cookie now
Response.Write(myCookie.Values.ToString());
