. Net demonstration of various cookie operations

Source: Internet
Author: User

This demo mainly introduces various. net operations on cookies, such as modification, deletion, and clearing. For more information, see. net operations.

The Code is as follows: Copy code

Imports System. Web. HttpContext
Public Class CookieFramework
'Write a single Cookie
Public Shared Function WriteCookie (ByVal CookieName As String, ByVal CookieValue

As String, ByVal ExpiresDate As Integer) As Boolean
Dim aCookie As New HttpCookie (CookieName)
ACookie. Value = CookieValue
ACookie. Expires = DateTime. Now. AddDays (ExpiresDate)
System. Web. HttpContext. Current. Response. Cookies. Add (aCookie)
End Function

'Add subitem to the Cookie set
Public Shared Function WriteCookies (ByVal CookieName As String, ByVal CookieItem

As String, ByVal ItemValue As String, ByVal ExpiresDate As Integer) As Boolean
Dim aCookie As HttpCookie
If Current. Request. Cookies (CookieName) Is Nothing Then
ACookie = New HttpCookie (CookieName)
Else
ACookie = Current. Request. Cookies (CookieName)
End If
ACookie. Values (CookieItem) = ItemValue
ACookie. Expires = DateTime. Now. AddDays (ExpiresDate)
System. Web. HttpContext. Current. Response. Cookies. Add (aCookie)
End Function
'Read a single Cookie
Public Shared Function ReadCookie (ByVal CookieName As String) As String
If Current. Request. Cookies (CookieName) Is Nothing Then
Return Nothing
Else
Return Current. Request. Cookies (CookieName). Value
End If
End Function

'Read the subitem content in the Cookie set
Public Shared Function ReadCookies (ByVal CookieName As String, ByVal CookieItem

As String) As String
If Current. Request. Cookies (CookieName) Is Nothing Then
Return Nothing
Else
If Current. Request. Cookies (CookieName). Values (CookieItem) Is Nothing Then
Return Nothing
Else
Return Current. Request. Cookies (CookieName). Values (CookieItem)
End If
End If
End Function
'Delete the entire Cookie
Public Shared Function DeleteCookie (ByVal CookieName As String) As Boolean
Dim aCookie As New HttpCookie (CookieName)
Dim I As Integer
Dim limit As Integer = Current. Request. Cookies. Count-1
For I = 0 To limit
ACookie = Current. Request. Cookies (I)
ACookie. Expires = DateTime. Now. AddDays (-1)
Current. Response. Cookies. Add (aCookie)
Next
End Function
'Delete the subitem in the Cookie set.
Public Shared Function DeleteCookies (ByVal CookieName As String, ByVal ItemName

As String) As Boolean
Dim aCookie As HttpCookie = Current. Request. Cookies (CookieName)
ACookie. Values. Remove (ItemName)
ACookie. Expires = DateTime. Now. AddDays (1)
Current. Response. Cookies. Add (aCookie)
End Function
End Class

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.