Using Cookies in ASP.NET

Computers & TechnologyInternet

  • Author Yossi Sigura
  • Published November 21, 2010
  • Word count 538

Cookie is a small text file that browsers allow sites and Web pages to save on the users' hard disk.

Cookie is an effective and easy way to save user-specific information.

For example, if your site allows visitors to select the background color of a page then it would be a good idea to keep this color on the visitor's hard disk with the help of a cookie, so each time the visitor enters your site you can access the data that you stored in the cookie and change accordingly the background color of the page.

Web sites cookies does not associated with a particular page. This means that you can write cookies from one page

and read the same cookie from another page altogether. Sometimes developers use this to pass data from page to page.

At this point let me urge you not to use Cookies for this purpose. To transfer variables from page to page there are other, more effective, methods. for example by using Query Strings or using Session Variables or global variables.

My recommendation is to Use Cookies to store unique information on visitors computers in order to use it the next time they visit our site, so we will be able to suit them personally.

Please note that there is no limit to the number cookies a single site can create on a visitor's computer.

If you will review the code below you could see I chose to add an expiration date for the cookie I created. It's not a must do to add an expiration date to a cookie but it is a feature commonly used.

When a user revisits a site his browser checks the site's cookies. In case the a cookie has expired the browser does not send the cookie to the server and it deletes it from the visitor's hard disk.

Before we will actually write some code you must know that there is file size limit to cookies and it is about the maximum of 4096 bytes.

Our code to create a new cookie will look like this:

' Create a new cookie and initiate it

Dim NewCookie as httpCookie = New HttpCookie ("Details")

' Add values to the new cookie

New Cookie.Values.Add("Name", "Jon")

New Cookie.Values.Add("LastName", "Doe")

'Add Expiration date to the cookie

newCookie.Expires = #2/7/2010#

' Write the cookie to the visitors hard disk

Response.Cookies.Add(NewCookie)

In the code above I created a cookie and named it "Details". The cookie I created has two values: "first name" and "last name"

I also set an expiration date for the cookie.

The next time the user visits our website we will ask the browser to send us the cookies that we saved on his computer.

The code for reading the cookie Will be:

Dim sName as String = Request.Cookies("Details") ("Name")

Dim sLastName as string = Request.Cookies("Details") ("LastName")

Summary

Use cookies to keep specific information of visitors to your site. Remember that there are limitations to using Cookies.

among other things there is one limitation that can not be overcome and that's the user's ability to prevent cookies to

be written to his hard disk by defining "No cookies allowed" in his browser.

Yossi Sigura is a senior software engineer and a Cloud Computing expert with a passion for programming and programming languages. Yossi writes a few programming blogs among them are: http://www.codeanan.co.il and http://www.devschool.co.il

Article source: https://articlebiz.com
This article has been viewed 592 times.

Rate article

Article comments

There are no posted comments.

Related articles