Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Setting A Cookie in ASP.NET (http://www.go4expert.com/articles/setting-cookie-aspnet-t14605/)

pradeep 18Oct2008 10:53

Setting A Cookie in ASP.NET

What Is A Cookie

A cookie is a small text file sent to a web user's computer by a website. A cookie can be used to identify that user to the website on their next visit. Common uses include remembering login data, user preferences, and providing favorites lists.

How Does A Cookie Work

HTTP cookies are used by Web servers to differentiate users and to maintain data related to the user during navigation, possibly across multiple visits. When an user visits/logs in to go4expert.com the web server sets a cookie to track the user's last visted time, and some ID to track the specific visitor. Many websites also use cookies for personalization based on users' preferences.

How Much Can We Store In A Cookie

Cookie specifications suggest that browsers should support a minimal number of cookies or amount of memory for storing them. In particular, an internet browser is expected to be able to store at least 300 cookies of four kilobytes each (both name and value count towards this 4 kilobyte limit), and at least 20 cookies per server or domain, so it is not a good idea to use a different cookie for each variable that has to be saved. It's better to save all needed data into one single cookie.

Setting A Cookie in ASP.Net

Setting cookies with a ASP.Net script is easy, we just need to use the HttpCookie class in the System.Web namespace. Let's look at an example on creating a cookie with ASP.Net

Code: C#

//Creating the cookie
 HttpCookie sampleCookie = new HttpCookie("User");
 sampleCookie.Values.Add("Name", strUserName);
 sampleCookie.Values.Add("CountryCode", strCountry);
 sampleCookie.Expires = #01/01/2009#; // set the expiry
 Response.Cookies.Add(sampleCookie); // Add it to the header
 //Getting a cookie value
 String sGetCookie;
 strUserName = Request.Cookies("User")("Name").ToString();
 strUserCountry = Request.Cookies("User")("CountryCode").ToString();



JGRobinson 16Nov2008 00:06

Re: Setting A Cookie in ASP.NET
Excellent and very clear article - The only thing which I would add is that by default cookies are only sent for the domain which created them

Therefore if the cookie was written by test.go4expert.com it could not be read by forums.go4expert.comThe way of fixing this is to set the domain of the cookie to the base domain, then both forums.go4expert.com and test.go4expert.com could access the information

The code to add would be :-


Response.Cookies("domain").Value = DateTime.Now.ToString()
Response.Cookies("domain").Expires = DateTime.Now.AddDays(1)
Response.Cookies("domain").Domain = "go4expert.com"

I've tripped over this before and thought it was worth clarifyng.

zaanrider 18Nov2008 11:35

Re: Setting A Cookie in ASP.NET
Very nice artical..thanks

All times are GMT +5.5. The time now is 22:47.