What is HttpCookie Class in C#?
Ask by ICSM Computer
Updated 05 May 2025
The
HttpCookieclass in ASP.NET (System.Web) is used to create, read, and manage cookies — small pieces of data stored on the client’s browser.Cookies are commonly used for storing user preferences, authentication tokens, session identifiers, or any small data that needs to persist between requests.
Namespace and Assembly
Difinition
This class represents an individual cookie — a name-value pair optionally with metadata like expiration date, path, domain, and security settings.
Constructors
Common Properties
NameValueExpiresDomainPathSecuretrue, the cookie is sent only over HTTPSHttpOnlytrue, JavaScript cannot access the cookieValuesCreating and Sending a Cookie to the Browser
Reading a Cookie
This reads the cookie from the user's browser on the next request.
Deleting a Cookie
You "delete" a cookie by setting its expiration date in the past:
Security Notes
HttpOnly = trueto prevent client-side JavaScript from accessing cookies.Secure = truefor HTTPS-only cookies.Session vs Persistent Cookie
Expiresset — deleted when the browser closesExpiresset — stored until expirationExample Scenario: Remember User's Language Preference