---
title: "Cookies in ASP.NET"  
description: "In this blog, I’m explaining the concept of cookies in asp.net.  Cookies provide a way to store user-specific data. Cookies are known as many names HT"  
author: "Anchal Kesharwani"  
published: 2014-08-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/669/cookies-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# Cookies in ASP.NET

In this blog, I’m explaining the concept of cookies in asp.net.\

Cookies provide a way to store user-specific data. Cookies are known as many names HTTP Cookie, Response Cookie, web Cookie and [Browser Cookie](https://answers.mindstick.com/qa/103642/what-is-a-browser-cookie) and more. Cookies are one of several ways to store data about [web site](https://www.mindstick.com/articles/12285/the-finest-web-site-design-trends-you-may-expect-in-2017) visitors during the time when [web server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) and browser are not connected. Cookie is small [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file) sent by the web server and stored by the client machine to [hard disk](https://answers.mindstick.com/qa/102443/how-do-external-ssds-differ-from-traditional-hard-disk-drives). Cookies may be used for authentication, identification of a user session, user's preferences, [shopping cart](https://answers.mindstick.com/qa/30832/what-is-shopping-cart-abandonment) contents, or anything else that can be accomplished through storing [text data](https://answers.mindstick.com/qa/112298/how-do-i-implement-sentiment-analysis-in-text-data). The purpose of cookie store user-specific information with in user system to reduce burden on server.

##### There are two major types of cookie:

· Persistent cookie

· Non-persistent cookie

##### Persistent Cookie

Cookies are stored on [your computer](https://www.mindstick.com/articles/13023/choosing-the-best-power-cord-for-your-computer-a-layman-s-guide) hard disk. They stay on your hard disk and can be accessed by [web servers](https://www.mindstick.com/forum/160105/how-does-ajax-enable-asynchronous-communication-with-web-servers) until they are deleted or have expired.

##### Non-persistent Cookie

Cookies are saved only your [web browser](https://www.mindstick.com/blog/301688/best-web-browsers-for-2023) is running. They can be used by a web server only until you close your browser. They are not saved on your disk. A cookie has expired time expired that is Non-persistent cookie.

##### There are following limitations for cookies:

· Size of cookies is limited to 4096 bytes.

· Total 20 cookies can be used on a single website; if you exceed this browser will delete older cookies.

· End user can stop accepting cookies by browsers, so it is recommended to check the users’ state and prompt the user to enable cookies.

##### Example

##### Step 1

Create a form as given below:

![Cookies in ASP.NET](https://www.mindstick.com/blogs/3b08aefc-fae9-4d98-8a5f-707099ec7439/images/ca9ee082-ae1b-4262-9d81-853c2a3495fa.png)

##### Step 2

Write the code in webForm1.aspx.cs:

```
using System;using System.Web;public partial class webForm1 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }    protected void btnSave_Click(object sender, EventArgs e)    {        HttpCookie studentObj = new HttpCookie("StudentInfo");        studentObj["RollNumber"] = txtStudentRollNuber.Text;        studentObj["Name"] = txtStudentName.Text;        studentObj["Age"] = txtStudentAge.Text;        studentObj["Country"] = ddlCountry.SelectedItem.ToString();        studentObj["State"] = ddlState.SelectedItem.ToString();        studentObj["City"] = ddlCity.SelectedItem.ToString();        studentObj["Pincode"] = txtStudentPincode.Text;        studentObj.Expires = DateTime.Now.AddMinutes(20);        Response.Cookies.Add(studentObj);        lblMsg.Text = "Data has been Saved";    }    protected void btnClear_Click(object sender, EventArgs e)    {        txtStudentRollNuber.Text = "";        txtStudentName.Text = "";        txtStudentAge.Text = "";        ddlCountry.SelectedIndex = 0;        ddlState.SelectedIndex = 0;        ddlCity.SelectedIndex = 0;        txtStudentPincode.Text = "";        lblMsg.Text = "";    }    protected void btnRead_Click(object sender, EventArgs e)    {        HttpCookie readCookie = Request.Cookies["StudentInfo"];        if (readCookie != null)        {            txtStudentRollNuber.Text = readCookie["RollNumber"];            txtStudentName.Text=readCookie["Name"];            txtStudentAge.Text=readCookie["Age"];            ddlCountry.SelectedItem.Text = readCookie["Country"];            ddlState.SelectedItem.Text = readCookie["State"];            ddlCity.SelectedItem.Text = readCookie["City"];            txtStudentPincode.Text = readCookie["Pincode"];            lblMsg.Text = "";        }    }}
```

##### Step 3

Run the application:

Fill the data and click save button.

![Cookies in ASP.NET](https://www.mindstick.com/blogs/3b08aefc-fae9-4d98-8a5f-707099ec7439/images/ea97b60a-904a-47b8-95b4-3cc9a6d08ae6.png)

##### Step 4

Click Clear button to clear.

![Cookies in ASP.NET](https://www.mindstick.com/blogs/3b08aefc-fae9-4d98-8a5f-707099ec7439/images/a970523d-3dfe-4b87-ba5d-b5d203f1102a.png)

##### Step 5

Click on Read button:

![Cookies in ASP.NET](https://www.mindstick.com/blogs/3b08aefc-fae9-4d98-8a5f-707099ec7439/images/e41c2774-311f-44ac-93c1-bad056f0cf1a.png)

---

Original Source: https://www.mindstick.com/blog/669/cookies-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
