In this post I'm going to explain what are cookies and how to use them as a part of the ASP.NET client side state management. You can read my previous posts in the state management subject in the following links:
- Client side state management introduction
- ViewState technique
- Hidden fields technique
- Query strings technique
What are Cookies?
A cookie is a piece of data that is saved in the client web browser. The cookie is saved either in the memory of the web browser or as a text file in the file system of the client. Cookies are used to identify a user, to store state information, preferences of the user and etc. The ASP.NET use the cookies mechanism to track users session.
How Does Cookies Mechanism Work?
The mechanism of cookies is simple. When a client request a web page from a server the first request isn't containing a cookie. The server identify that the client has no cookie and generate one. Then the server sends the cookie to
the client and from now on the client will send the cookie in every request and the server will send the cookie in every response.
- Most browsers support cookies of up to 4096 bytes. This limitation makes the cookies a way to store only small amount of data.
- Browsers have a limit on the amount of cookies a web site can save in the client. Most browsers allow only 20 cookies per site.
- The user can set the browser to disable cookies and therefore you can't trust cookies and you always have to check if the browser enables cookies.
Cookies are sent in the HTTP header of the response. To create a cookie you need to use the Response.Cookies property. To read a cookie value from the client use the Request.Cookies property. The following example
shows how to read a cookie named "cookie" and how to write the cookie:
// it can be different from null only is
// a request was made before
if (Request.Cookies["cookie"] != null)
// get the cookie
HttpCookie cookie = Request.Cookies["cookie"];
// get the cookie value
string value = Server.HtmlEncode(cookie.Value);
// write the cookie to the response
Response.Cookies["cookie"].Value = "value";
In the HttpCookie class there is a Expires property. If you don't use it (like in the example) the cookie is saved in memory and discarded if the user closes the browser. If you use the property you must define the amount of time the cookie is saved. In order to delete a saved cookie you need to use the Expires property and pass a past expiration date. There is no way to delete a cookie otherwise. In the previous example the cookie saves a single value. The cookie can save multiple values like I show in the next example.
Response.Cookies["cookie"]["key1"].Value = "value1";
Response.Cookies["cookie"]["key2"].Value = "value2";
Response.Cookies["cookie"]["key3"].Value = "value3";
Determine If a Browser Accepts Cookies
The clients can disable cookies. One way to check if cookies are disabled is to write a cookie to the Response and in the next Request to check if the cookie exists. If the cookie doesn't exists you need to assume that cookies are disabled.
To sum up the post, I explained what are cookies and how you can use them to save state. The cookies technique is one of the backbones of the ASP.NET session state which will be covered in a later post. The last client side state management technique - the control state - will be explained in the next post of this series.