In this article, we will learn about working with Creating,Reading and Deleting Cookies in Asp.Net.
Introduction
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.
Objective
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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Working with Cookies</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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" />
</div>
</form>
</body>
</html>
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()
{
try
{
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)
{
create_user_cookies();
}
}
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;
}
}
Output:
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;
}
}
Conclusion
In this article, we have learned about Cookies and their Usage.
Reference
http://geekcamp.us/articles/delete_all_the_cookies_created_by_a_website_in_aspnet.aspx