Using Cookies


ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 23.  State Management in ASP.NET

If you do not wish to store the state of your application on the Web server, you can send the state out to the client's browser. You do this by using cookies. You will most likely use a memory cookie because they are destroyed when users close their browsers. To create a memory cookie, you use the Cookies property of the Response object, as shown in the following code. In this example, the code creates a memory cookie named Email and assigns the e-mail address into the cookie:

 Response.Cookies("Email").Value = "" 

Another method of creating a new cookie is to use the Add method of the Cookies collection, which is a property of the Response object. You can create a new System.Web.HttpCookie object and pass in the name and the value to the constructor for that object. Here's an example:

 Response.Cookies.Add(New _  System.Web.HttpCookie("Email", "")) 

Either method shown here will set a memory cookie in the user's browser for you. This assumes that the user allows memory cookies in his browser.

To retrieve a memory cookie on any subsequent page, or even on the same page, use the Request object. You should first check to see whether the memory cookie has been created yet. If you try to access the Cookies collection and pass in the name of a variable that has not yet been created, you will receive a runtime error:

 If Not Request.Cookies("Email") Is Nothing Then    txtEmail.Text = Request.Cookies("Email").Value End If 

The sample code checks to see whether the Request.Cookies("Email") object is Nothing. If it is, you won't be able to do anything with that cookie. If the object has something in it, you can retrieve the value and assign it to a text box on the form.

Permanent Cookies

If you wish to store a cookie to a user's hard disk, you need to set the Expires property on that cookie. The following code shows an example of how you create a cookie called EmailPerm and set the expiration date for 30 days in the future:

 With Response.Cookies("EmailPerm")   .Value = txtEmail.Text   .Expires = Today.AddDays(30) End With 

By setting the Expires property to a date in the future, the browser stores this cookie in a folder on the user's hard disk. Users may set their browsers to only allow memory cookies and not allow permanent cookies. If the active browser won't allow permanent cookies, the data can't be stored. You will not receive an error that the cookie could not be stored, but you won't get the data back when you request the cookie. If you wish to remove a permanent cookie, set the Expires property to a date in the past.

Issues with Cookies

Using cookies gives you excellent state management capabilities because they are simple to implement and help you move resources off the server. Like almost any particular technique, cookies have some limitations:

  • Some users don't allow cookies. Some users believe that viruses can be sent in a cookie and will not allow them on their computers. Although there have never been any documented cases of this happening (and no one could realistically send a virus through a cookie), a lot of users still turn off the ability to accept cookies. When this happens, the user will not be able to use your site if you use cookies for managing state.

  • Performance can deteriorate. Imagine that a user walks through a wizard on your site, and you gather 100 pieces of data from that user over several pages. Each page needs to post gathered data to the server. If you wait until all 100 pieces of data are gathered, you need to store that data somewhere in the meantime. If you keep putting data into a cookie, a lot of data is being sent back and forth between the browser and the server. This will eat up a lot of bandwidth and could slow your whole site down. Remember, the data has to go both ways for each page the user hits on your site.

  • Cookies take up memory. Some browsers impose a limit on the size of the cookie data they can accept or the number of cookies they can accept at one time. In addition, the amount of memory that you may chew up on the user's machine may cause his operating system to swap some memory to disk. Under this circumstance, the cookie has slowed down your user's machine as well as the server.


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234 © 2008-2017.
    If you may any questions please contact us: