I wrote this out of necessity, and unfortunately I wrote it in VB. I thought someone other than me could benefit from it.
I hope to change this to act more like a datatable, soon, but I needed something fast. Enjoy!
Imports System.Web
Imports System.Web.UI
Imports System.Collections.Specialized
Public Class CookieReader
Private Sub New()
End Sub
#Region "[ Normal Cookies ]"
'''
''' Gets a cookie from the client browser.
'''
'''Cookie name.
'''
''' The value of the cookie as a String.
'''
Public Shared Function GetValue(ByVal Name As String) As String
If Not CookieReader.Get(Name) Is Nothing Then
If Not CookieReader.Get(Name).Value Is Nothing Then
Return CookieReader.Get(Name).Value
Else
Return String.Empty
End If
End If
End Function
'''
''' Sets a cookie on the client browser
'''
'''Cookie name.
'''Cookie value.
Public Shared Sub [Set](ByVal Name As String, ByVal Value As String)
CookieReader.Set(Name, Value, Nothing, String.Empty)
End Sub
'''Cookie name.
'''Cookie value.
'''Cookie path.
Public Shared Sub [Set](ByVal Name As String, ByVal Value As String, ByVal Path As String)
CookieReader.Set(Name, Value, Nothing, Path)
End Sub
'''Cookie name.
'''Cookie value.
'''Cookie expiration date.
Public Shared Sub [Set](ByVal Name As String, ByVal Value As String, ByVal Expires As DateTime)
CookieReader.Set(Name, Value, Expires, String.Empty)
End Sub
'''Cookie name.
'''Cookie value.
'''Cookie expiration date.
'''Cookie path.
Public Shared Sub [Set](ByVal Name As String, ByVal Value As String, ByVal Expires As DateTime, ByVal Path As String)
Dim cookie As HttpCookie
Dim isNew As Boolean = False
If HttpContext.Current.Request.Cookies(Name) Is Nothing Then
cookie = New HttpCookie(Name)
isNew = True
Else
cookie = HttpContext.Current.Request.Cookies(Name)
End If
With cookie
.Value = Value
.Expires = Expires
.Path = Path
End With
If isNew Then
HttpContext.Current.Response.Cookies.Add(cookie)
Else
HttpContext.Current.Response.Cookies(Name).Value = Value
End If
End Sub
'''
''' Removes the cookie from the client browser.
'''
Public Shared Sub Remove(ByVal Name As String)
If Not HttpContext.Current.Request.Cookies(Name) Is Nothing Then
HttpContext.Current.Request.Cookies(Name).Expires.AddDays(-1)
HttpContext.Current.Request.Cookies.Remove(Name)
End If
End Sub
#End Region
#Region "[ Nested Cookies ]"
Public Shared Function GetValue(ByVal ParentCookie As HttpCookie, ByVal Name As String) As String
If ParentCookie.Values.Count > 0 Then
If Not ParentCookie.Values.Get(Name) Is Nothing Then
Return ParentCookie.Values.Get(Name)
End If
End If
End Function
Public Shared Sub [Set](ByVal ParentCookieName As String, ByVal Cookies As HttpCookie())
CookieReader.Set(ParentCookieName, Cookies, Nothing, String.Empty)
End Sub
Public Shared Sub [Set](ByVal ParentCookieName As String, ByVal Cookies As HttpCookie(), ByVal Expires As DateTime)
CookieReader.Set(ParentCookieName, Cookies, Expires, String.Empty)
End Sub
Public Shared Sub [Set](ByVal ParentCookieName As String, ByVal Cookies As HttpCookie(), ByVal Path As String)
CookieReader.Set(ParentCookieName, Cookies, Nothing, Path)
End Sub
Public Shared Sub [Set](ByVal ParentCookieName As String, ByVal Cookies As HttpCookie(), ByVal Expires As DateTime, ByVal Path As String)
Dim parentCookie As HttpCookie
Dim childCookie As HttpCookie
Dim currentCookie As HttpCookie
Dim isNewCookie As Boolean
' create or retrieve cookies
If CookieReader.Get(ParentCookieName) Is Nothing Then
parentCookie = New HttpCookie(ParentCookieName)
isNewCookie = True
Else
parentCookie = CookieReader.Get(ParentCookieName)
isNewCookie = False
End If
' set expiration and path
parentCookie.Expires = Expires
parentCookie.Path = Path
' set the cookies
For Each currentCookie In Cookies
If parentCookie.Values.Get(currentCookie.Name) Is Nothing Then
parentCookie.Values.Add(currentCookie.Name, currentCookie.Value)
Else
parentCookie.Values.Set(currentCookie.Name, currentCookie.Value)
End If
Next
' apply cookies
If isNewCookie Then HttpContext.Current.Response.Cookies.Add(parentCookie)
End Sub
Public Shared Function [Get](ByVal ParentCookieName As String) As HttpCookie
If Not HttpContext.Current.Request.Cookies(ParentCookieName) Is Nothing Then
Return HttpContext.Current.Request.Cookies(ParentCookieName)
End If
End Function
#End Region
End Class