OLAP

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

Chrome-Cookies

Here, you can set your settings, if you want to see your cookies, click on ‘All cookies and site data’.

Chrome-CookieCollection

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 message.

We have completed technical part of cookies and will move to some more advance features.

Cookie Age

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!!

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI 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 ...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • VB.Net: Download file from FTP and UnZipVB.Net: Download file from FTP and UnZipExperience: Many application requires data migration tasks to execute on daily basis. To achieve this we generally seek help of SSIS package, if at all we are making use of SQL sever. ...