Work With Cookies

Cookies are an alternative to using Session objects if you want to share data from page to page. If this data is very sensitive and you do not want your user to see this data, cookies are not a good option. Cookies are stored in plain text on the user's local machine.

After you have created the cookie on the user's machine, you can then access the cookie from another page. After the ASP.NET page finds the value of the cookie, you can have the page perform an action depending on the value. A Request.Cookies statement enables you to read a cookie. You must know the name of the cookie you want to read. If the cookie you want to read does not exist on the user's computer, the value of the cookie will be null.

A cookie can use subkeys to store several related values. For example, you could create a cookie that stores the user's font preferences. For this cookie, you could store both the preference for the font size and for the font name. With subkeys, you can do this using one cookie. To add a subkey to the cookie, use the Values.Add method for the HTTPCookie object.

WORK WITH COOKIES (CONTINUED)

WORK WITH COOKIES (CONTINUED)

_• Click where you want to create a cookie and type HttpCookie cookieUserlnfo = new HttpCookie("cookie UserFontPreferences");.

Set another the cookie subkey by typing cookieUserlnfo.Values. Add("FontName","Verdana");.

le 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.

_• Click where you want to create a cookie and type HttpCookie cookieUserlnfo = new HttpCookie("cookie UserFontPreferences");.

Set the cookie subkey by typing cookieUserlnfo. Values.Add("FontSize","8pt");.

Set another the cookie subkey by typing cookieUserlnfo.Values. Add("FontName","Verdana");.

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

le 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

You can loop through the subkeys of a cookie, which enables you to work with the subkeys as a collection, perhaps displaying the names and values of the subkeys in the cookie.

TYPE THIS:

HttpCookieCollection cookie collectionAll

For (int loop=0: loop <cookie collectionAll.Count; loop ++) {HttpCookie cookie = cokkie collectionAll(loop); If (cookie.HasKeys)

{NameValueCollection namevaluecollection = new NameValueCollection(cookie.Values);

String.[] StringValueNames = namevaluecollection.All Keys;

String {} StringValues = namevaluecollection.All;

RESULT:

FontSize = 8pt FontName = Verdana

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

le Open ReadCookie CollectionTemplate.aspx from the Code Templates directory, click where you want to read a cookie, and type HttpCookie cookieUserlnfo = Request.Cookies["cookieUser FontPreferences"];.

£ Read the contents of the subkey in the cookie into a variable that will style the page by typing stringFont Size = cookieUserlnfo. Values["cookieFontSize"];.

le To read the second subkey, type stringFont Name = cookieUserlnfo. Values["FontName"];.

Save the page as Read CookieCollection.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.

Was this article helpful?

0 0

Post a comment