Reading Cookies

You use the Response.Cookies collection to create and modify cookies. You use the Request.Cookies collection to retrieve a cookie’s value.

For example, the page in Listing 28.3 retrieves the message cookie’s value.

Listing 28.3. GetCookie.aspx

images

In Listing 28.3, the IsNothing() function checks whether the cookie exists before reading its value. If you don’t include this check, you might get a null reference exception. Also, don’t forget that cookie names are case-sensitive.

The page in Listing 28.4 lists all cookies contained in the Request.Cookies collection (see Figure 28.2).

Figure 28.2. Displaying a list of all cookies.

Get ASP.NET 4 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.