Basics of Cookies is Asp.Net

Through this article you are able to know about the following points:
1> What are the cookies?
2> How to write and read cookies
3> Limitation of cookies

1>What are cookies?

Let us take an example. You request for a page from your site, say, and your application sends not only a page but also a text file containing date and time. When you get the page through your browser, you also get that text file which will be stored in a particular folder of your hard drive. Later when you type again in the address bar and click ‘Go’, the browser will search your local hard drive for the text file associated with this typed URL. If that text file exists, browser will send it along the request. So the website will understand the date and time when this user last visited. This text file is called cookies.
So, the proper definition of a cookie will be –
Cookies are small text files stored on hard drive of user by web server. They contain information the server can read whenever user visits the site and/or request for a particular page from that site.

2>How to write and read cookies:
a>Create & Set (Write) a cookie:

The two most important things to create a cookie are

•Give a proper name to the cookie and set its value.
•Set the time of expiration of that cookie

          Let, in a web page there is a textbox named txtUserName and it is used to store name of a user. The following code will help you to create a cookie named userName which contains the text in txtUserName textbox and will expire after 10days of creation.

           Response.Cookies("userName").Value = txtUserName.Text.Trim();
           Response.Cookies("userName").Expires = DateTime.Now.AddDays(10);

Remember,if user deletes the cookie, then web server will not found it in user’s hard drive even if the cookie will not cross its expiration time.

Alternative way to Create & Set (Write) a cookie:

           HttpCookie  myCookie = new HttpCookie ("userName");
           myCookie.Value = txtUserName.Text.Trim();
           myCookie.Expires = DateTime.Now.AddDays(10);

b>Read the value of cookie:
The two most important things to create a cookie are

•Check whether the value of cookie is null or not.
•Set the value of Cookie in a variable or any other controls like textbox etc.

          Recall the previous example. Now you try to retrieve the username from the cookie “username” and set the value to a textbox named txtClientName of the another webpage of same web server. The code will be as follows:

           if (Request.Cookies (“username”) != null)   
       txtClientName.Text = Request.Cookies(“username”).Value;

Remember, if you do not check whether the cookie is present or not then you’ll get System.NullReferenceException exception when cookie is not present.
 Alternative way to Read the value of a cookie:
          if (Request.Cookies (“username”) != null)
                 HttpCookie  myCookie = Request.Cookies ("userName"); 
                 txtClientName.Text = myCookie.Value;

3>Limitation of cookies:

•Previously told, user can remove cookies at anytime from their hard drives so the expiration time should not be taken to a standard parameter of a cookie to expire.

•Many users can disable their browser to receive cookies for security reason. In that case use of cookie is meaningless.

•You cannot store large information of user in a cookie as cookies are used to store limited text like User ID or any other small identifiers. Most browsers support 4096 bytes for cookies.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

Which of the following is a must before you step out of your home?

Which of the following is a must before you step out of your home?
No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 6 answer(s).
[{"id":"22790","title":"A lip balm","votes":"5","type":"x","order":"1","pct":7.81,"resources":[]},{"id":"22791","title":"A pair of goggle","votes":"1","type":"x","order":"2","pct":1.56,"resources":[]},{"id":"22792","title":"An umbrella","votes":"4","type":"x","order":"3","pct":6.25,"resources":[]},{"id":"22793","title":"Wrist watch","votes":"4","type":"x","order":"4","pct":6.25,"resources":[]},{"id":"22794","title":"Cell phone","votes":"48","type":"x","order":"5","pct":75,"resources":[]},{"id":"22795","title":"Comb","votes":"2","type":"x","order":"6","pct":3.13,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.
Your online reputation is important You should know that it takes decades to make your reputation but to ruin it you need just couple of minutes and this
  Introduction A Web server hosts information in the form of HTML files. The Web server is usually passive and merely acts as an efficient interface
Best language to learn .NET language is the best thing to learn. It has somewhat similar syntax as java and it’s a Microsoft Technology moreover it
If you decide to have an  opt-in list is not just a matter of sending your subscribers your promotional newsletters or catalogs. There are several things
To follow this step: 1. Download Asp.Net 3.5 framework sp1 and install it. 2. Download AspNetMVC1 then install it. 3. For Nunit test download nunitProjecttemplate