Work With Cookies

You can use ASP.NET to create cookies from an ASP.NET page. When the user views the page, the cookie is stored as a small text file on the user's computer. A cookie consists of a key, which indicates the name of the cookie, and a value, which is the information stored in the cookie.

When you create a cookie, you should specify when the cookie will expire. By default, a cookie is usually deleted as soon as the user closes his or her Web browser. Setting an expiration date for a cookie enables the cookie to store information for longer periods of time. Most Web browsers store all the cookies they receive in one folder. The storage location depends on the Web browser installed on the computer.

Typically, you will write the cookie to the user's browser before you begin sending any HTML in the response to the client. If you write a cookie midway through sending back a response, you may get an error. This depends on whether you have buffering of responses enabled on your page or for the application. Buffering the response for the entire application can be set in the IIS MMC. For IIS 5.0, buffering is turned on by default.

WORK WITH COOKIES

WORK WITH COOKIES

-Q Open CreateCookie Template.aspx from the Code Templates directory, click where you want to create a cookie, and type HttpCookie cookieUserlnfo = newHttpCookie("cookieUser HasVisited"); and press Enter.

0 Set the cookie value by typing cookieUserlnfo.Value "Yes";.

Save the page as CreateCookie.aspx to the Default Web site and display the ASP.NET page in a Web browser.

■ The Web browser displays a message about creating a cookie.

-Q Open CreateCookie Template.aspx from the Code Templates directory, click where you want to create a cookie, and type HttpCookie cookieUserlnfo = newHttpCookie("cookieUser HasVisited"); and press Enter.

0 Set the cookie value by typing cookieUserlnfo.Value "Yes";.

To write the cookie to the Web browser, type Response.AppendCookie (cookieUserlnfo);.

Save the page as CreateCookie.aspx to the Default Web site and display the ASP.NET page in a Web browser.

■ The Web browser displays a message about creating a cookie.

Note: The cookie remains available until the Web browser is closed.

ASP.NET APPLICATIONS AND STATE MANAGEMENT

App

ir

If you do not set an expiration date for a cookie, the Web browser stores the cookie only until the user closes the Web browser. In most cases, you should keep this information for a longer period of time. To do this, you can use the Expires property of your cookie. You can use the DateTime object's Now property, along with some convenient functions that add time to the current date and time.

r F

It

TYPE THIS:

TYPE THIS:

HttpCookie cookieUserInfo = new HttpCookie("cookieUserHasVisited");

cookieUserInfo.Value = "Yes";

cookieUserInfo.Expires = DateTime.Now.AddDays(7).ToString();

Response.AppendCookie(cookieUserInfo);

HttpCookie cookieUserInfo = new HttpCookie("cookieUserHasVisited");

cookieUserInfo.Value = "Yes";

cookieUserInfo.Expires = DateTime.Now.AddMonths(1).ToString();

Response.AppendCookie(cookieUserInfo);

%>

V

V

RESULT:

RESULT:

The cookie is set for a week.

The cookie is set for a month.

J

0 Open ReadCookie Template.aspx from the Code Templates directory.

0 Read and display the cookie contents by typing Response.Write(Request. Cookies["cookieUserHas Visited"].Value);.

_Q Save the page as ReadCookie.aspx to the default Web site and display the ASP.NET page in a Web browser.

0 Open ReadCookie Template.aspx from the Code Templates directory.

0 Read and display the cookie contents by typing Response.Write(Request. Cookies["cookieUserHas Visited"].Value);.

_Q Save the page as ReadCookie.aspx to the default Web site and display the ASP.NET page in a Web browser.

■ The Web browser displays a message about the contents of the cookie.

Q Open CreatCookie CollectionTemplate.aspx from the Code Templates directory. HOU^

Was this article helpful?

0 0

Post a comment