State Management using Cookies.

What are Cookies?

Cookies are nothing but a small piece of information that are stored at the client’s browser by the server. This small piece of information about the user is then sent by the browser in all subsequent requests to the same URL in the request. We can store anything in a cookie; General example of cookie usages are to store the user preferences, password remembering, storing user options, etc.

Generally cookies are stored a plain text files in the local disk of the user. Cookies can be accessed from anywhere in the application. Generally Cookies are lost after the user closes the browser but we can also have cookies that will persist even after browser is closed.

Cookies are mainly classified into two types:

  1. persistant
  2. non-persistant.

Persistant cookies:

As the name itself suggests, these cookies remain persistant in the client’s memory even after the browser is closed. They remain permanently in memory till they are explicitly removed or their expiration is reached.

Non-persistant cookies: These cookies do not remain in the client’s memory and are lost after browser is closed.

How to create cookies from code:

Generally we use the HttpCookie class to create an instance of a cookie for that session, then add the values that we want to be included in the cookie as key-value pairs. The below code explains this more clearly.

HttpCookie userCookie = new HttpCookie("infoCookie");
userCookie["username"] = "Amogh";
userCookie["City"] = "Hyderabad";
userCookie["Country"] = "India";

//adding the cookie to the Response object. This will be sent to the client on first request from the client.
Response.Cookies.Add(userCookie);

The example shown above is that of a NONpersistent cookie as it doesn’t specify any expiration time for the cookie. If we add an expiration to the cookie object, the cookie will become a persistent cookie. Example code is shown below.

HttpCookie userCookie = new HttpCookie("infoCookie"); 
userCookie["username"] = "Amogh";
userCookie["City"] = "Hyderabad";
userCookie["Country"] = "India";

//adding an expiration to the cookie
userCookie.Expires = DateTime.Now.AddDays(1);

//adding the cookie to the Response object. This will be sent to  the client on first request from the client.

Response.Cookies.Add(userCookie);

The “Expires” property of the HttpCookie object specifies that the cookie will expire after 1 day of its creation.

Reading values from cookie: Values from cookie can be read directly by using the Request object of the current HttpContext.

HttpCookie infoCookie = Request.Cookies["infoCookie"];
string city = infoCookie["City"];

Advantages of Cookies :

  1. Very easy to use.
  2. Browser takes care of sending and maintaining cookies from multiple sites

Disadvantages of cookies:

  1. Not secure as data is stored as plain text
  2. cookie size is limited to 4KB
  3. Some browsers may not support cookies. So care needs to be taken while using cookies in code.

Hope this helps!!

Leave a Reply