How does one read ASP cookies in ASP. NET?

Source: Internet
Author: User

 

When a browser sends a request to the server, the cookie of the server is sent together with the request. Application in ASP. NET Program You can use the request object to read the cookie. The structure of the request object is basically the same as that of the response object. Therefore, the method for reading cookies from the request object is similar to that for writing cookies to the response object. The following example shows two methods to obtain the cookie value named "username" and display the value in the label control:

If not request. Cookies ("username") is nothing then
Label1.text = server. htmlencode (request. Cookies ("username"). value)
End if

If not request. Cookies ("username") is nothing then
Dim acookie as httpcookie = request. Cookies ("username ")
Label1.text = server. htmlencode (acookie. value)
End if

Before obtaining the cookie value, make sure that the cookie exists. Otherwise, you will get a system. nullreferenceexception (English) exception. Before displaying the cookie content on the page, I call the httpserverutility. htmlencode (English) method to encode the cookie content. This is because I want to display the content of the cookie (generally you do not) and make sure that no malicious user adds executable scripts to the cookie. For more information about Cookie security, see cookies and security.

Note: Because different browsers store cookies in different ways, different browsers on the same computer may not be able to read their cookies from each other. For example, if you use Internet Explorer to test a page and then use another browser for testing, the latter will not find the cookie saved by Internet Explorer. Of course, most people generally use the same browser for web interaction, so in most cases there will be no problems. However, you may still encounter problems, such as testing the browser compatibility of applications.

The method for reading the cookie neutron key value is similar to that for setting this value. The following is a method to obtain the subkey value:

If not request. Cookies ("userinfo") is nothing then
Label1.text = _
Server. htmlencode (request. Cookies ("userinfo") ("username "))
Label2.text = _
Server. htmlencode (request. Cookies ("userinfo") ("lastvisit "))
End if

In the above example, I obtained the value of the subkey "lastvist". In the previous discussion, I set this value to the string representation of the datetime value. Remember that cookies store values in the form of strings. Therefore, to use the lastvisit value as a date, you must convert it:

Dim dT as datetime
Dt = cdate (request. Cookies ("userinfo") ("lastvisit "))

The cookie subkey type is a set of namevaluecollection types. Therefore, another way to obtain a single sub-key is to first obtain the sub-key set and then extract the sub-key value by name, as shown below:

If not request. Cookies ("userinfo") is nothing then
Dim userinfocookiecollection _
System. Collections. Specialized. namevaluecollection
Userinfocookiecollection = request. Cookies ("userinfo"). Values
Label1.text = server. htmlencode (userinfocookiecollection ("username "))
Label2.text = server. htmlencode (userinfocookiecollection ("lastvisit "))
End if

Just like setting a cookie, you can decide which method to use to read the cookie.

What is the validity period?

You can read the name and value of the cookie. In addition, you need to know a lot about the cookie. Although you can obtain the domain and path attributes, these attributes have limited usage. 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.

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

In the previous section on cookie writing, I have already mentioned that the browser is responsible for Cookie management, and the expires attribute is a good example. The expires attribute is mainly used to help the browser perform daily management of cookie storage. From the server perspective, the cookie either exists or does not exist. Therefore, the validity period is not useful for the server. Therefore, the browser does not provide this information when sending cookies. If you need the cookie expiration date, you must reset it. I will introduce this in modifying and deleting cookies.

More specifically, you can read the expires attribute set in the response object before sending the cookie to the browser, but you cannot get the validity period information from the returned request object.

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.