---
title: "Cookies in C#"  
description: "This article provides an overview of working with cookies in ASP.NET applications. I will show you the mechanics of working with cookies in ASP.NET."  
author: "David Miller"  
published: 2016-05-06  
updated: 2018-03-27  
canonical: https://www.mindstick.com/articles/12044/cookies-in-c-sharp  
category: "c#"  
tags: ["c#", "cookies"]  
reading_time: 2 minutes  

---

# Cookies in C#

This article provides an overview of working with cookies in ASP.NET applications. I will show you the mechanics of working with cookies in ASP.NET. Along the way, I will explain various features and (occasional) oddities of cookies and the support in ASP.NET for them.

Cookies provide a useful means in [Web applications](https://www.mindstick.com/blog/302483/full-stack-development-the-complete-guide-to-building-modern-web-applications) to store user-specific information. For example, when a user visits our site, we can use cookies to store [user preferences](https://www.mindstick.com/forum/159311/implement-a-react-component-that-sorts-and-filters-data-based-on-user-preferences) or other information. When the user visits your [Web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017) another time, the application can retrieve the information it stored earlier.

A cookie is a small bit of text that accompanies requests and pages as they go between the [Web server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) and browser. The cookie contains information the [Web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about) can read whenever the user visits the site.

Imagine that when users request a page from [your site](https://www.mindstick.com/articles/12865/infographic-drive-traffic-to-your-site-in-2018-10-hints), www.mindstick.com, [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding) sends not just a page, but a cookie containing the date and time. When the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's [hard disk](https://answers.mindstick.com/qa/102443/how-do-external-ssds-differ-from-traditional-hard-disk-drives).

\

**Example:** below provided a [source code](https://www.mindstick.com/forum/23271/is-there-a-way-to-get-the-source-code-from-an-apk-file) for Cookie.

## Cookies.aspx file

```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies.aspx.cs" Inherits="Session" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">        <div>            <asp:Label ID="lblRecord" runat="server" /> <br />                        <asp:Button ID="btnSetCookie" runat="server" Text="SetCookie" OnClick="btnSetCookie_Click" />            <asp:Button ID="btnPrintCookie" runat="server" Text="Print Cookie" OnClick="btnPrintCookie_Click" />         </div>    </form></body></html>
```

**Cookies.aspx.cs file** \

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class Cookies : System.Web.UI.Page{    HttpCookie cook;    protected void Page_Load(object sender, EventArgs e)    {        cook=new HttpCookie("MyCookie");    }       protected void btnSetCookie_Click(object sender, EventArgs e)    {        cook.Value = "MyCookie";    }    protected void btnPrintCookie_Click(object sender, EventArgs e)    {        lblRecord.Text = cook.Name;    }} 
```

\

\

\

\

---

Original Source: https://www.mindstick.com/articles/12044/cookies-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
