DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET - Cookies - Client Side State Management

ASP.NET - Cookies - Client Side State Management

Gil Fink user avatar by
Gil Fink
·
Jul. 23, 08 · News
Like (0)
Save
Tweet
Share
24.20K Views

Join the DZone community and get the full member experience.

Join For Free

In this post I'm going to explain what are cookies and how to use them as a part of the ASP.NET client side state management. You can read my previous posts in the state management subject in the following links:

  • Client side state management introduction
  • ViewState technique
  • Hidden fields technique
  • Query strings technique

What are Cookies?


A cookie is a piece of data that is saved in the client web browser. The cookie is saved either in the memory of the web browser or as a text file in the file system of the client. Cookies are used to identify a user, to store state information, preferences of the user and etc. The ASP.NET use the cookies mechanism to track users session.

How Does Cookies Mechanism Work?


The mechanism of cookies is simple. When a client request a web page from a server the first request isn't containing a cookie. The server identify that the client has no cookie and generate one. Then the server sends the cookie to
the client and from now on the client will send the cookie in every request and the server will send the cookie in every response.

Cookies Limitations

  • Most browsers support cookies of up to 4096 bytes. This limitation makes the cookies a way to store only small amount of data.
  • Browsers have a limit on the amount of cookies a web site can save in the client. Most browsers allow only 20 cookies per site.
  • The user can set the browser to disable cookies and therefore you can't trust cookies and you always have to check if the browser enables cookies.

A rule of thumb - do not use cookies for critical information.

How To Use Cookies?

Cookies are sent in the HTTP header of the response. To create a cookie you need to use the Response.Cookies property. To read a cookie value from the client use the Request.Cookies property. The following example
shows how to read a cookie named "cookie" and how to write the cookie:

// it can be different from null only is
// a request was made before

if (Request.Cookies["cookie"] != null)
{
   // get the cookie
   HttpCookie cookie = Request.Cookies["cookie"]; 

   // get the cookie value
   string value = Server.HtmlEncode(cookie.Value);
} 

// write the cookie to the response
Response.Cookies["cookie"].Value = "value";

In the HttpCookie class there is a Expires property. If you don't use it (like in the example) the cookie is saved in memory and discarded if the user closes the browser. If you use the property you must define the amount of time the cookie is saved. In order to delete a saved cookie you need to use the Expires property and pass a past expiration date. There is no way to delete a cookie otherwise. In the previous example the cookie saves a single value. The cookie can save multiple values like I show in the next example.

Response.Cookies["cookie"]["key1"].Value = "value1";
Response.Cookies["cookie"]["key2"].Value = "value2";
Response.Cookies["cookie"]["key3"].Value = "value3";

Determine If a Browser Accepts Cookies


The clients can disable cookies. One way to check if cookies are disabled is to write a cookie to the Response and in the next Request to check if the cookie exists. If the cookie doesn't exists you need to assume that cookies are disabled.

Summary


To sum up the post, I explained what are cookies and how you can use them to save state. The cookies technique is one of the backbones of the ASP.NET session state which will be covered in a later post. The last client side state management technique - the control state - will be explained in the next post of this series.

ASP.NET

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mr. Over, the Engineer [Comic]
  • Simulate Network Latency and Packet Drop In Linux
  • Do Not Forget About Testing!
  • Educating the Next Generation of Cloud Engineers With Google Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: