Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Base Blockchain
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
0
Answer
CookieReader class - a little quirky, but better than what we've got
tsuman_in
21y
3.1k
1
Reply
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
Post
Reset
Cancel
Answers (
0
)
Next Recommended Forum
HowTO sort objects without IComparible?
How to get frames from HTMLDocument