Tutorials » AspNet tutorials »

Working with Cookies in ASP.NET

This chapter talks about storing and retrieving cookies in ASP.NET

ASP.NET has 3 classes which allow you to work with Cookies.

HttpCookie: provides a way to create and manipulate individual HTTP cookies.
HttpResponse: The Cookies property allows to create and save cookies on client machines.
HttpRequest: The Cookies property allows access to cookies from the client maintains.

How to create a Cookie ?

It is really easy to create a cookie in ASP.NET. You can use the Response object in ASP.NET to create cookies as key-value pairs.

Response.Cookies("UserName").Value = "Mr. John"
Response.Cookies("UserName").Expires = DateTime.Now.AddDays(1)

In the above sample code, the first line will save a cookie with the name "UserName" and value "Mr. John" to the client computer.

The second line says that this cookie is valid for 1 more day from current time.

As shown above, you can store any key-value pairs to client computer as cookies.

How to retrieve values from Cookies ?

You can use the Request object to retrieve values from the cookies which are already stored.

Dim name As String = Me.context.Request.Cookies("UserName").Value

If there is no cookie with the name "UserName", the above code sample will return an empty string.

The HttpCookie class

HttpCookie is another usefull class in ASP.NET which allows you to work with Cookies. Here is a code snippet which shows how to use this class:

Dim myCookie As New HttpCookie("UserName")
myCookie.Value = "Mr. John"
myCookie.Expires = DateTime.Now.AddDays(1)
myCookie.Domain = "aspspider.com"

How cookies are stored and retrieved ?

You saw the sample code which uses Request and Response objects to store and retrieves values in Cookies. Do you know how they really work?

When you execute the line Response.Cookies("UserName").Value = "Mr. John", it runs on the server. It has no connection with the client computer. Then how is it saving on the client computer?

When your browser request a URL from the web server, what it gets back is a 'big packet'. This packet has lot of information which includes the meta data, cookies, content of the page to be displayed to you etc. So, the cookies is part of the response the browser gets back from the web server. Browser knows what to do with each piece of data in the response. It saves the cookies to your local computer, and displays the page content in your browser.

Now, you might have understood why you use Response.Cookies("UserName").Value = "Mr. John" to save cookies to client computer. You are actually adding a cookie to the response which will be sent to the browser.

The same thing happens when you retrieve cookies from the Request object. Everytime when browser makes a request, it sends a "big packet" to the web server. This big packet contains lot of information including the URL requested, details about the brower itself and all the cookies stored in the client computer from that website. This is why you are able to retrieve the cookie information from the "Request" object.

Next Chapter: Configuration File for .NET applications
Previous Chapter: What is Cookies in the web world
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index

Top Contributors
    Last 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India