Ehsan Ghanbari

Experience, DotNet, Solutions

How many types of cookies are there in

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);
    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";
    5.             //Persistent
    6.             var httpCookie1 = Response.Cookies["Form"];
    7.             if (httpCookie1 != null) ResponseCookies.Expires = DateTime.Now.AddHours(4);
    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


About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In MVC
Multiple submit buttons in MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus