Asp.Net: More about Cookies

Welcome to CodeSpread! 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. We have two objects, HttpResponse and HttpRequest in our application life cycle which handles the use of cookies. For writing information to a cookie, we would require HttpRespone object. This object contains a collection of cookies where we just have to add our cookie. 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. System.Web 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. string shoppingcart=Request.Cookies[“ShoppingCart”].Value; OfCourse, Check for Null Value of shoppingcart before using it in your application else you might receive a beautiful error...

Read More

Asp.Net: Hidden Truth of Cookies

I know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their expiry? If they contain user centric information then Can they be encrypted? Lets explore each of my question then we will go more into coding part of cookies. I remember the first definition of cookie that i learned “Cookie is a text file which is stored at the user’s hard disk and contains user’s session information” This definition is very much synonymous to Google definition: “A small sweet cake, typically round, flat, and crisp.” Good.Right? Our Cookie is a small file like small cake and provided sweet feeling by streamlining our session information. But when i really started working on cookies, I had my share of queries and resolutions like I came to know that as ‘http’ is a stateless protocol so we always have to attach our session information. What is this session information and why it is required every time? Well, the best answer is user expectations, we always want our web to be extra smart.Don’t we? if i say, you don’t have to sign in to your Gmail account on each request or you don’t have to add the items on your shopping basket every time or you just want your website to tell you,when you visited the site last time. This is the smart decisions we expect from our website but as HTTP protocol is stateless and can’t retain our information then what it did, it provided a Cookie mechanism to store the information at user’s side and send it to server on each request where it gets modified as per the request and sent back to the user to store it again. Here, I’ll sweetify the definition a bit: “Cookie is a piece of client’s information kept at client’s side in the form of a text file and , on each request and response, this information travels between the server and client” No files travels between server and client, only information travels in between server and client. At client side, information is read by the ‘HTTP’ request from this text file and while the response is received, this file is written with the updated information again. So where this file is located, It depends on the browser so let’s believe what we see now. In FireFox Run this: %AppData%\Mozilla\Firefox\Profiles\%FirefoxProfile%\cookies.sqlite To read the data you need to connect to that database and query the DB In Internet Explorer Run this : %AppData%\Microsoft\Windows\Cookies In Chrome Run this: %AppData%\\Google\Chrome\User Data\Default\Cookies Now, we...

Read More