Blog

Filter posts by Category Or Tag of the Blog section!

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

Wednesday, 12 June 2013

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 website. A cookie is one of the several ways to store data in the user's browser when the user is not connected to the web server which is sent by a 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 usages of Cookies is traveling 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 website 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 websites, all of them could send a cookie to your browser and store them separately. Cookies are also using 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 website or not. Because of the 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 the 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 they 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 texted 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 the Persistent cookie will be expired as soon as the application is closed or when the expire time will be 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 an expiry date and stored in browser memory.

 

How to implement cookie? 

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

 

           //How to create a cookie via HttpCookie Class, this is a persistent cookie because it has a expire date
            var httpCookie = new HttpCookie("Form") {Expires = DateTime.Now.AddHours(3)};
            Response.Cookies.Add(httpCookie);

            //how to read this Cookie
            var cookie = Request.Cookies["Form"];
            if (cookie != null)
            {
                string form = cookie.Value;
            }


            //how to create cookie via Response , Non Persistent
            var ResponseCookies = Response.Cookies["Form"];
            if (ResponseCookies != null) ResponseCookies.Value = "A value";

            //Persistent
            var httpCookie1 = Response.Cookies["Form"];
            if (httpCookie1 != null) ResponseCookies.Expires = DateTime.Now.AddHours(4);
        
            //how to read 
            string responseCookie = Request.Cookies["httpCookie1"].Value;

            //How to Delete a cookie before it's expiration time
            ResponseCookies.Expires = DateTime.Now.AddHours(-4);

            //how to create Multiple Values in a cookie
            Response.Cookies["MyCookie"]["CustomerName"] = "Ehsan";
            Response.Cookies["MyCookie"]["CustomerFamily"] = "Ghanbari";
            Response.Cookies["MyCookie"]["CustomerAge"] = "24";
            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

Category: Software

Tags: Asp.Net

comments powered by Disqus