---
title: "Cookies in ASP.Net"  
description: "Cookie is a small text file sent by web server and saved by web browser on client machine. Cookies are created when a user's browser loads a particula"  
author: "Dev Vrat Sahu"  
published: 2012-09-01  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/989/cookies-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Cookies in ASP.Net

Cookie is a small [text file](https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file) sent by [web server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) and saved by [web browser](https://answers.mindstick.com/qa/95505/what-are-the-pros-and-cons-of-using-safari-web-browser) on client machine. [Cookies](https://www.mindstick.com/articles/40/cookies-in-asp-dot-net) are created when a user's browser loads a particular [website](https://yourviews.mindstick.com/view/87705/when-do-we-require-an-agency-to-drive-traffic-to-a-website). The website sends information to the browser which then creates a text file. Every time the user goes back to the same website, the browser retrieves and sends this file to the website's server.\

##### There are two types of cookies available in .Net

\

1. Non [Persistent](https://www.mindstick.com/interview/22840/what-is-the-use-of-persistent-store-coordinator-in-core-data) / [Session](https://www.mindstick.com/articles/12042/session-in-c-sharp) Cookies
2. Persistent Cookies

##### Non Persistent / Session Cookies

Non Persistent cookies are stored in memory of the client browser session, when browsed is closed the non persistent cookies are lost.

##### Persistent Cookies

Persistent Cookies have an expiration date and theses cookies are stored in Client [hard drive](https://answers.mindstick.com/qa/114546/how-to-troubleshoot-external-hard-drive-recognition-issues).

##### Example:

Following example will show how to store the Cookies and [Retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) Cookies Data.

**1.** Create the UI in [ASP.Net](https://www.mindstick.com/articles/155/concept-of-asp-dot-net) as shown bellow

![Cookies in ASP.Net](https://www.mindstick.com/mindstickarticle/1183ed16-00f6-4bee-8790-01e7254ee673/images/689fcbb3-33e0-41d2-bf28-97f9418e4a04.png)

**2.** Now, switch to Code View and write the following codes

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page{      protected void Page_Load(object sender, EventArgs e)    {     }    //Storing the Cookies    protected void Button1_Click(object sender, EventArgs e)    {        //Creating Object for HttpCookie Class and Named it "UserInfo"        HttpCookie objCookie = new HttpCookie("UserInfo");         objCookie["UserName"] =TextBox1.Text.ToString();        objCookie["Password"] =TextBox2.Text.ToString();        //Specified time limit forcookie to accessed        objCookie.Expires= DateTime.Now.AddMinutes(30);         Response.Cookies.Add(objCookie);    }     //Reading data from Cookies    protected void Button2_Click(object sender, EventArgs e)    {               HttpCookie objRequestRead =Request.Cookies["UserInfo"];        if (objRequestRead!= null)            Response.Write(objRequestRead["UserName"] + " " + objRequestRead["password"]);        else        {            Response.Write("Cookies has beenExpired");            return;        }     }     //Reading all stored cookies    protected void Button3_Click(object sender, EventArgs e)    {    
   HttpCookieCollection objAllRequestRead = Request.Cookies;        for (int i = 0; i < objAllRequestRead.Count; i++)        {            HttpCookie ck = objAllRequestRead.Get(i);            if (ck != null)            {                Response.Write("Cookie Name "+"'" + ck.Name +"'");                Response.Write("<br> Cookie Value " + ck.Value);                Response.Write("<br>");           }        }    }}
```

Cookies are stored for 24 hrs, if you have not specified any time limit for Cookies Expiration.

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