Working with Creating,Reading and Deleting Cookies in Asp.Net.

Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 5492 red flag

In this article, we will learn about working with Creating,Reading and Deleting Cookies in Asp.Net.


As we know that,Web or HTTP is a state-less protocol,so maintaining state is not easy.Because in every post back or every request to the server,values are lost.So it's required to maintain a state.So,if there will be multiple requests,then we will still have our data.

ASP.NET provides several ways of maintaining state across multiple requests among which Cookies is one of the options.

Cookies are one of several ways to store data.These are small pieces of text,stored on the client's computer.
Each time the browser requests a page from the server it sends the relevant cookies too.
Asp.Net allows you to create and retrieve cookies easily.

In this article we will see how to create,read and delete all the cookies created by the website at once.


Working with Creating,Reading and Deleting Cookies in ASP.NET.

Using the code

Creating a Cookie:-
We use Response.Cookies to create a Cookie.

For example:-
Response.Cookies["User_Name"] = "Vishal";

Retrieving Cookie:-
We the help of Request.Cookies method,we can retrieve Cookies.

For example:-
string user_name = Convert.ToString(Request.Cookies["User_Name"]);

We can understand Cookies by an Example:-

Our aspx page contains only 2 buttons named Read Cookies and Delete Cookies as shown below:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies.aspx.cs" Inherits="Cookies" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Working with Cookies</title>
<form id="form1" runat="server">
<asp:Button ID="btn_read_cookies" runat="server" Text="Read Cookies" OnClick="btn_read_cookies_Click" />
<asp:Button ID="btn_delete_cookies" runat="server" Text="Delete Cookies" OnClick="btn_delete_cookies_Click" />

Now,in Code-Behind,we will create Cookies as shown below:-

In Page Load event we will call Create_User_Cookies method as shown:-

    private void create_user_cookies()
            Response.Cookies["firstname"].Value = "Vishal";
            Response.Cookies["firstname"].Expires = DateTime.Now.AddYears(1);
            Response.Cookies["lastname"].Value = "Kumar";
            Response.Cookies["lastname"].Expires = DateTime.Now.AddYears(1);
            Response.Cookies["age"].Value = "30";
            Response.Cookies["age"].Expires = DateTime.Now.AddYears(1);
            Response.Cookies["city"].Value = "Mumbai";
            Response.Cookies["city"].Expires = DateTime.Now.AddYears(1);
            Response.Cookies["state"].Value = "Maharashtra";
            Response.Cookies["state"].Expires = DateTime.Now.AddYears(1);
        catch (Exception ex)
            throw ex;
protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

Now,in Read Cookies Button event,we will populate user information as shown:-

//reads and loads user information from cookies
private void read_user_information_cookies() { try { Response.Write("Use First Name:" + Request.Cookies["firstname"].Value + "
"); Response.Write("Use Last Name:" + Request.Cookies["lastname"].Value + "
"); Response.Write("Use Current Age:" + Request.Cookies["age"].Value + "
"); Response.Write("Use Current City:" + Request.Cookies["city"].Value + "
"); Response.Write("Use Current State:" + Request.Cookies["state"].Value + "
"); } catch (Exception ex) { throw ex; } } protected void btn_read_cookies_Click(object sender, EventArgs e) { try { read_user_information_cookies(); } catch (Exception ex) { throw ex; } }


Now,in Delete Cookies Button event,we will delete all the Cookies in one shot which are stored

With the help of Request.Cookies.AllKeys, we can have all the Cookies as shown:

// delete all the Cookies
private void delete_all_cookies() { try { string[] cookies = Request.Cookies.AllKeys; foreach (string cookie in cookies) { Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1); } } catch (Exception ex) { throw ex; } } protected void btn_delete_cookies_Click(object sender, EventArgs e) { try { delete_all_cookies(); read_user_information_cookies(); } catch (Exception ex) { throw ex; } }


In this article, we have learned about Cookies and their Usage.


Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: vishal kumar
Member Level: Platinum
Member Status: Member,MVP
Member Since: 11/5/2013 5:58:17 AM
Country: India

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)