Asp. Basic knowledge of cookie programming in net (4)

Source: Internet
Author: User
Tags datetime domain
asp.net|cookie| Programming Read Cookies

When the browser sends a request to the server, the server's Cookie is sent with the request. In the ASP.net application, you can use the Request object to read cookies. The request object's structure is essentially the same as the structure of the Response object, so the method of reading the cookie from the request object is very similar to the method of writing cookies to the Response object. The following example shows two ways to get the value of a Cookie named "username" and display the value in a Label control:

If not Request.Cookies ("UserName") are nothing Then
Label1.Text = Server.HTMLEncode (Request.Cookies ("UserName"). Value)
End If

If not Request.Cookies ("UserName") are nothing Then
Dim Acookie as HttpCookie = Request.Cookies ("UserName")
Label1.Text = Server.HTMLEncode (acookie.value)
End If

Before getting the value of a cookie, you should ensure that the cookie does exist. Otherwise, you will get an System.NullReferenceException (English) exception. Also note that before displaying the contents of the cookie in the page, I called the HttpServerUtility.HtmlEncode (English) method to encode the contents of the cookie. This is done because I want to display the contents of the cookie (which you would not normally do) and make sure that no malicious user has added an executable script to the cookie. For more information about cookie security, see cookies and security.

Note: Because different browsers store cookies differently, different browsers on the same computer will not necessarily be able to read each other's cookies. For example, if you use Internet Explorer to test a page and then test it with another browser, the latter will not find cookies saved by Internet Explorer. Of course, most people typically use the same browser for Web interaction, so in most cases there is no problem. However, there are times when you are experiencing problems, such as testing your application for browser compatibility.

The method of reading the neutron key value of a Cookie is similar to the method for setting the value. Here's a way to get a subkey value:

If not Request.Cookies ("UserInfo") are nothing Then
Label1.Text = _
Server.HTMLEncode (Request.Cookies ("UserInfo") ("UserName"))
Label2.Text = _
Server.HTMLEncode (Request.Cookies ("UserInfo") ("Lastvisit"))
End If

In the example above, I get the value of the subkey "Lastvist", which I set to the string representation of the DateTime value in the previous discussion. Keep in mind that cookies hold values in the form of strings, so you must convert the Lastvisit value as a date:

Dim DT as DateTime
DT = CDate (Request.Cookies ("UserInfo") ("Lastvisit"))

The type of the Cookie's neutron key is a collection of NameValueCollection (English) types. Thus, another way to get a single subkey is to get a set of subkeys and then extract the values of the subkeys by name, as follows:

If not Request.Cookies ("UserInfo") are nothing Then
Dim UserInfoCookieCollection As _
System.Collections.Specialized.NameValueCollection
UserInfoCookieCollection = Request.Cookies ("UserInfo"). Values
Label1.Text = Server.HTMLEncode (UserInfoCookieCollection ("UserName"))
Label2.Text = Server.HTMLEncode (UserInfoCookieCollection ("lastvisit"))
End If

As with cookies, it is up to you to decide which method to use to read cookies.

What is the expiration date?

You can read the name and value of the cookie, and there is not much information you need to know about cookies. Although you can get Domain and Path properties, these properties are of limited use. For example, you can read the domain attribute, but if your page is not in the same domain as the cookie, you will not receive the cookie at the page location at all.

What you cannot read is the expiration date and time of the Cookie. In fact, when the browser sends Cookie information to the server, the browser does not include expired information. You can read the Expires property, but always return a date/time value of zero.

As I've already said in the previous section on writing cookies, the browser is responsible for managing cookies, and the Expires attribute is a good proof of that. The primary role of the Expires property is to help the browser perform day-to-day management of the Cookie store. From the server's point of view, the Cookie either exists or does not exist, so the expiration date is not useful information for the server. Therefore, the browser does not provide this information when sending cookies. If you need the expiration date of the cookie, you must reset it, and I will introduce it in the Modify and delete cookie.

Rather, you can read the Expires property that has been set in the Response object before sending a Cookie to the browser, but you cannot get the expiration information from the returned Request object.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.