Asp.Net: More about Cookies
We have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part.
Properties and Limitations
Cookies are associated with website and not single pages.
Cookie is stored in our system as Text File.
They are stored in plain text format i.e. no encryption.
Their size on disk is limited to 4096 bytes. so only identifiers and ids are stored, big chunks of data are avoided.
Browsers puts a limitation on the number of cookies stored. In case of more than the quota, old cookies are discarded.
Nowadays, It is mandatory for websites to tell the user whether they will allow cookies to be stored so It depends on the user to allow or reject the cookie
Even we can change our browser settings to allow or reject cookies.
Go to your chrome settings->privacy->content settings
Here, you can set your settings, if you want to see your cookies, click on ‘All cookies and site data’.
We will move to technical part of cookies.
First of all, we will try to create a cookie and store some information.
Write a Cookie
As always, we require necessary classes to begin with and for that we require a Namespace.Here, we have System.Web, which will expose necessary classes.
Now, We will use HttpCookie class to create our cookie
HttpCookie ShoppingCart = new HttpCookie("ShoppingCart"); ShoppingCart.Value = ItemTextBox.Text; ShoppingCart.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(ShoppingCart);
Earlier, we talked about HttpResponse but here we are using Response object. why? Both are same, HttpResponse is available to us in the form Response property of our Page so we are directly using Response object.
Though we have other ways also to create cookies but this is the descriptive way to understand each step. As Response.Cookies is a collection, we can add more than one cookie to the collection by creating multiple cookies in similar fashion.
Read a Cookie
To read a cookie, we have HttpRequest object and this is also available in the form of Request property of our page. Lets use it and read the cookie. We just have to remember the name of our cookie and dig it out from the cookies collection of Request object.
OfCourse, Check for Null Value of shoppingcart before using it in your application else you might receive a beautiful error message.
We have completed technical part of cookies and will move to some more advance features.
We can easily define the age of a cookie as we have two options available.
Persistent cookies: If you have observed that once you logged in your email account and your browser accepts cookies then you don’t need to log in again till the time cookies are cleared. This is possible with our permanent cookies, which are stored in the client machine until they are cleared or expired. We have already created one persistent cookie in our above example. It can be done by setting expiration time.
ShoppingCart.Expires = DateTime.Now.AddHours(1);
Non-persistent Cookies: These are called temporary Cookies and they exists till the time user closes their browser as they are stored in browser’s memory.
The only difference between them is persistent cookies should have expiration defined.
We will see more advance concepts and also look at questions floating around on cookies in our next article.Wait for that!!