Read Cookie set
The preceding example assumes that you want to read a Cookie with a known name. Sometimes, you may need to read all the cookies available for the page. To read the names and values of all Cookies available for the page, you can use the following code to traverse the Request. Cookies set:
Dim I As Integer
Dim output As String = ""
Dim aCookie As HttpCookie
For I = 0 to Request. Cookies. Count-1
ACookie = Request. Cookies (I)
Output & = "Cookie Name =" & Server. HtmlEncode (aCookie. Name) & "<br>"
Output & = "Cookie Value =" & Server. HtmlEncode (aCookie. Value )&_
& "<Br>"
Next
Label1.Text = output
Note: When you run this code, you may see a Cookie named "ASP. NET_SessionId". ASP. NET uses this Cookie to save the unique identifier of your session. This session Cookie will not be permanently stored on your hard disk. For more information about session cookies, see cookies and session status.
The preceding example has a restriction: If a Cookie has a subkey, the subkey is displayed with a separate name/value string. The HasKeys attribute of a Cookie tells you whether the Cookie has a subkey. If you have a sub-key, you can drill down in the sub-key set to obtain the names and values of each sub-key.
As described above, you can obtain information about subkeys from the Cookie property Values (English), which is a collection of NameValueCollection types. You can directly read the sub-key value from the Values set based on the index value. The corresponding sub-key value can be obtained from the value set Member AllKeys (English), this member will return a string set.
The following example shows how to modify the previous example. In this example, the HasKeys attribute is used to test the subkey. If the subkey is detected, the subkey is obtained from the Values set:
Dim I As Integer
Dim j As Integer
Dim output As String = ""
Dim aCookie As HttpCookie
Dim subkeyName As String
Dim subkeyValue As String
For I = 0 To Request. Cookies. Count-1
ACookie = Request. Cookies (I)
Output & = "Name =" & aCookie. Name & "<br>"
If aCookie. HasKeys Then
For j = 0 To aCookie. Values. Count-1
SubkeyName = Server. HtmlEncode (aCookie. Values. AllKeys (j ))
SubkeyValue = Server. HtmlEncode (aCookie. Values (j ))
Output & = "subkey name =" & subkeyName & "<br>"
Output & = "sub-key value =" & subkeyValue & "<br>"
Next
Else
Output & = "Value =" & Server. HtmlEncode (aCookie. Value) & "<br>"
End If
Next
Label1.Text = output
You can also extract the sub-key as a NameValueCollection object, as shown below:
If aCookie. HasKeys Then
Dim CookieValues _
System. Collections. Specialized. NameValueCollection = aCookie. Values
Dim CookieValueNames () As String = CookieValues. AllKeys
For j = 0 To CookieValues. Count-1
SubkeyName = Server. HtmlEncode (CookieValueNames (j ))
SubkeyValue = Server. HtmlEncode (CookieValues (j ))
Output & = "subkey name =" & subkeyName & "<br>"
Output & = "sub-key value =" & subkeyValue & "<br>"
Next
Else
Output & = "Value =" & aCookie. Value & "<br>"
End If
Note: Remember, the reason why I call the Server. HtmlEncode method is that I want to display the Cookie value on the page. If you only test the Cookie value, you do not need to encode it before use.
Modify and delete cookies
Sometimes, you may need to modify a Cookie, change its value or extend its validity period. (Remember, because the browser does not pass the validity period information to the server, you cannot read the Cookie expiration date .)
Of course, you do not actually directly change the Cookie. Although you can obtain and operate a Cookie from the Request. Cookies collection, the Cookie itself exists somewhere on the user's hard disk. Therefore, modifying a Cookie actually means creating a new Cookie with a new value and sending the Cookie to the browser to overwrite the old Cookie on the client.
The following example shows how to change the Cookie value used to store the number of site visits:
Dim counter As Integer
If Request. Cookies ("counter") Is Nothing Then
Counter = 0
Else
Counter = CInt (Request. Cookies ("counter"). Value)
End If
Counter + = 1
Response. Cookies ("counter"). Value = counter. ToString
Response. Cookies ("counter"). Expires = DateTime. Now. AddDays (1)
Or:
Dim ctrCookie As HttpCookie
Dim counter As Integer
If Request. Cookies ("counter") Is Nothing Then
CtrCookie = New HttpCookie ("counter ")
Else
CtrCookie = Request. Cookies ("counter ")
End If
Counter = CInt (ctrCookie. Value) + 1
CtrCookie. Value = counter. ToString
CtrCookie. Expires = DateTime. Now. AddDays (1)
Response. Cookies. Add (ctrCookie)