How many types of cookies are there in Asp.net?

What is a Cookie?

A cookie is a small text file that can be remembered between pages or web server and browser. web application read that file information whenever the user refers to the web site. Cookie is one of the several ways to store data in the user's browser when the user is not connect to the web server which is sent by web server to the browser.

 

What are the usages?

Cookies could be used for authentication, identification, shopping cart information, or any text data. One of the most usage of Cookies is travelling of data from one page to another. when a user visits your site, you can use cookies to store his/her information. Your Web server can retrieve the stored information whenever the user visits the web site again If the cookie exists. Cookies are associated with a Web site, not with a specific page, so the browser and server will exchange cookie information. As a user when you visit different web sites, all of them could send cookie to your browser and store them separately. Cookies are also useful a web server which wants to know when the users visit the web page. Cookies also can be used in a poll system, For example you recognize that which user has voted something in your web site or not. Because of small limit of storage, it's better to store small amounts of data in cookies. Most browsers allow only 20 cookies per site; if you try to store more cookies in user's browser, the oldest cookies are discarded.

 

How cookie works?

When the server sends a page to a browser, it also sends the cookie to the browser via the HttpResponse object that exposes a collection. Cookies are saved in a folder in the hard drive of user's machine. The browser is responsible for managing cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection(as you will see in the implementation). When creating a cookie, you specify a Name and Value. Each cookie must have a unique name so that it can be identified later; Because cookies are stored by name. All cookies are sent to the server with any request to that site. In other words, every page in a site gets all of the cookies for that site. You cannot directly modify a cookie. but you can create a new one with the same value and send to the user's browser, it will be replaced with the existing cookie.

 

Security in cookie

Cookies are purely text Not a software program which can be included a virus or something like that. but a cookie could be stolen by hackers and get used. some information like account, credit card information or etc. in a cookie could be stolen.

 

Kinds of cookie

There are two main types of cookies: session cookies and persistent cookies. When a user closes the web browser, session cookie information is automatically removed from the computer. but Persistent cookie will be expired as soon as the application is closed or when the expire time will ended. Persistent cookies(aka permanent cookies)are stored by the user's browser even when the browser has been closed. Persistent cookies have an expiry date and can be anything from a few minutes to several years. session cookies(also called Non-persistent and temporary Cookies) have expiry date and stored in browser memory.

 

How to implement cookie? 

Take a look at these implementation, you can see the descriptions as comments

 

    1.            //How to create a cookie via HttpCookie Class, this is a persistent cookie because it has a expire date
    2.             var httpCookie = new HttpCookie("Form") {Expires = DateTime.Now.AddHours(3)};
    3.             Response.Cookies.Add(httpCookie);
    4.  
    5.             //how to read this Cookie
    6.             var cookie = Request.Cookies["Form"];
    7.             if (cookie != null)
    8.             {
    9.                 string form = cookie.Value;
    10.             }

 

    1.             //how to create cookie via Response , Non Persistent
    2.             var ResponseCookies = Response.Cookies["Form"];
    3.             if (ResponseCookies != null) ResponseCookies.Value = "A value";
    4.  
    5.             //Persistent
    6.             var httpCookie1 = Response.Cookies["Form"];
    7.             if (httpCookie1 != null) ResponseCookies.Expires = DateTime.Now.AddHours(4);
    8.         
    9.             //how to read 
    10.             string responseCookie = Request.Cookies["httpCookie1"].Value;
 
    1.             //How to Delete a cookie before it's expiration time
    2.             ResponseCookies.Expires = DateTime.Now.AddHours(-4);
 
    1.             //how to create Multiple Values in a cookie
    2.             Response.Cookies["MyCookie"]["CustomerName"] = "Ehsan";
    3.             Response.Cookies["MyCookie"]["CustomerFamily"] = "Ghanbari";
    4.             Response.Cookies["MyCookie"]["CustomerAge"] = "24";
    5.             Response.Cookies["MyCookie"].Expires = DateTime.Now.AddHours(20);

 

Also Take a look at these two nice articles for more information

  1. http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx
  2. http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies


Tags: Asp.Net


comments powered by Disqus