---
title: "What is HttpCookie Class in C#?"  
description: "What is HttpCookie Class in C#?"  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34077/what-is-httpcookie-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# What is HttpCookie Class in C#?

The `HttpCookie` class 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

```cs
using System.Web;
```

## Difinition

```cs
public sealed class HttpCookie
```

This class represents an individual cookie — **a name-value pair** optionally with metadata like expiration date, path, domain, and security settings.

## Constructors

| [HttpCookie(String, String)](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcookie.-ctor?view=netframework-4.8.1#system-web-httpcookie-ctor(system-string-system-string)) | Creates, names, and assigns a value to a new cookie. |
| --- | --- |
| [HttpCookie(String)](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcookie.-ctor?view=netframework-4.8.1#system-web-httpcookie-ctor(system-string)) | Creates and names a new cookie. |

## Common Properties

| Property | Description |
| --- | --- |
| `Name` | The name of the cookie (required) |
| `Value` | The string value stored in the cookie |
| `Expires` | Sets the expiration date (otherwise it's a session cookie) |
| `Domain` | Specifies the domain the cookie is valid for |
| `Path` | Specifies the path the cookie is valid for |
| `Secure` | If `true`, the cookie is sent only over HTTPS |
| `HttpOnly` | If `true`, JavaScript cannot access the cookie |
| `Values` | A collection of key-value pairs inside the cookie |

## Creating and Sending a Cookie to the Browser

```cs
HttpCookie userCookie = new HttpCookie("UserSettings");
userCookie["Theme"] = "Dark";
userCookie["FontSize"] = "Medium";
userCookie.Expires = DateTime.Now.AddDays(7); // lasts 7 days
userCookie.HttpOnly = true;

Response.Cookies.Add(userCookie);
```

## Reading a Cookie

```cs
HttpCookie cookie = Request.Cookies["UserSettings"];
if (cookie != null)
{
    string theme = cookie["Theme"];
    string fontSize = cookie["FontSize"];
}
```

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:

```cs
if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie cookie = new HttpCookie("UserSettings");
    cookie.Expires = DateTime.Now.AddDays(-1); // expired
    Response.Cookies.Add(cookie);
}
```

## Security Notes

1. Set `HttpOnly = true` to prevent client-side JavaScript from accessing cookies.
2. Use `Secure = true` for HTTPS-only cookies.
3. Cookies can be intercepted if not encrypted — don’t store sensitive data unless encrypted.

## Session vs Persistent Cookie

| Type | Behavior |
| --- | --- |
| **Session cookie** | No `Expires` set — deleted when the browser closes |
| **Persistent cookie** | `Expires` set — stored until expiration |

## Example Scenario: Remember User's Language Preference

```cs
// On first login
HttpCookie langCookie = new HttpCookie("PreferredLang", "en-US");
langCookie.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(langCookie);

// Later usage var lang = Request.Cookies["PreferredLang"]?.Value;
```

## Answers

### Answer by ICSM Computer

The `HttpCookie` class 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

```cs
using System.Web;
```

## Difinition

```cs
public sealed class HttpCookie
```

This class represents an individual cookie — **a name-value pair** optionally with metadata like expiration date, path, domain, and security settings.

## Constructors

| [HttpCookie(String, String)](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcookie.-ctor?view=netframework-4.8.1#system-web-httpcookie-ctor(system-string-system-string)) | Creates, names, and assigns a value to a new cookie. |
| --- | --- |
| [HttpCookie(String)](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcookie.-ctor?view=netframework-4.8.1#system-web-httpcookie-ctor(system-string)) | Creates and names a new cookie. |

## Common Properties

| Property | Description |
| --- | --- |
| `Name` | The name of the cookie (required) |
| `Value` | The string value stored in the cookie |
| `Expires` | Sets the expiration date (otherwise it's a session cookie) |
| `Domain` | Specifies the domain the cookie is valid for |
| `Path` | Specifies the path the cookie is valid for |
| `Secure` | If `true`, the cookie is sent only over HTTPS |
| `HttpOnly` | If `true`, JavaScript cannot access the cookie |
| `Values` | A collection of key-value pairs inside the cookie |

## Creating and Sending a Cookie to the Browser

```cs
HttpCookie userCookie = new HttpCookie("UserSettings");
userCookie["Theme"] = "Dark";
userCookie["FontSize"] = "Medium";
userCookie.Expires = DateTime.Now.AddDays(7); // lasts 7 days
userCookie.HttpOnly = true;

Response.Cookies.Add(userCookie);
```

## Reading a Cookie

```cs
HttpCookie cookie = Request.Cookies["UserSettings"];
if (cookie != null)
{
    string theme = cookie["Theme"];
    string fontSize = cookie["FontSize"];
}
```

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:

```cs
if (Request.Cookies["UserSettings"] != null)
{
    HttpCookie cookie = new HttpCookie("UserSettings");
    cookie.Expires = DateTime.Now.AddDays(-1); // expired
    Response.Cookies.Add(cookie);
}
```

## Security Notes

1. Set `HttpOnly = true` to prevent client-side JavaScript from accessing cookies.
2. Use `Secure = true` for HTTPS-only cookies.
3. Cookies can be intercepted if not encrypted — don’t store sensitive data unless encrypted.

## Session vs Persistent Cookie

| Type | Behavior |
| --- | --- |
| **Session cookie** | No `Expires` set — deleted when the browser closes |
| **Persistent cookie** | `Expires` set — stored until expiration |

## Example Scenario: Remember User's Language Preference

```cs
// On first login
HttpCookie langCookie = new HttpCookie("PreferredLang", "en-US");
langCookie.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(langCookie);

// Later usage var lang = Request.Cookies["PreferredLang"]?.Value;
```


---

Original Source: https://www.mindstick.com/interview/34077/what-is-httpcookie-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
