BestCloudHostingASP.NET | Best and Affordable ASP.NET Hosting. Hello guys today I will explain Cookies in ASP.NET.
What’s Cookies ?
A cookie is a piece of information in the form of a very small text file placed on an internet user’s hard drive. It is generated by a web page server that is basically the computer that operates a web site. The user can also change the cookie before the browser sends it to you. So never store a password, username, email, pan number in cookies. We can declare at least 300 cookies per website and at least 4096 bytes per cookie.
Now the question is, how to declare a Cookie.
1st way
HttpCookie Cookies1 = new HttpCookie("Cookies1"); Cookies1.Value = txt1.Text; Response.Cookies.Add(Cookies1);
2nd way
Response.Cookies["Cookies1"].Value = txt1.Text;
3rd way
We can write multiple values in each cookie.
Response.Cookies["Cookies"]["Name"] = txt1.Text; Response.Cookies["Cookies"]["class"] = txt2.Text; Response.Cookies["Cookies"]["sec"] = txt3.Text; Response.Cookies["Cookies"]["Roll"] = txt4.Text;
How we access cookies
The following describes how to access cookies.
For 1st way and 2nd way we use:
String cooks=Request.Cookies[“Cookies”].Value;
And the 3rd way for multiple values in a single cookie:
String name=Request.Cookies["Cookies"]["Name"]; String class=Request.Cookies["Cookies"]["class"]; String sec=Request.Cookies["Cookies"]["sec"]; Int roll= String name=Request.Cookies["Cookies"]["roll"];
Now how to Delete a Cookie
Response.Cookies["Cookies1"].Expires = DateTime.Now.AddHours(-1);
Just provide a negative value for the time you add when declaring a Cookie.
I will describe with code:
First we create a page called CreateCookie.aspx as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateCookies.aspx.cs" Inherits="CreateCookies" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <b>Pleas enter your Name:</b> <br /><br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /><br /> <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click1"/> </div> </form> </body> </html>
CreateCookie.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
public partial class CreateCookies : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
//First Way
HttpCookie Cookies1 = new HttpCookie("Cookies1");
Cookies1.Value = TextBox1.Text;
Cookies1.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(Cookies1);
// Second Way
Response.Cookies["Cookies2"].Value = TextBox1.Text;
Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(1);
// Multiple values in single cookie
Response.Cookies["Cookies3"]["name"] = "AAFIA";
Response.Cookies["Cookies3"]["class"] = "class-ix";
Response.Cookies["Cookies3"]["sec"] = "A";
Response.Cookies["Cookies3"]["roll"] = "4";
Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(1);
Response.Redirect("ResultCookis.aspx");
}
}
Now we access cookies on another page called ResultCookie.aspx as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultCookis.aspx.cs" Inherits="ResultCookis" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <b>Now See Cookies Data Here: </b> <br /><br /> first way <br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br /> second way <br /> <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <br /> multiple Value in single Cookies <br /> <asp:Label ID="Label3" runat="server" Text=""></asp:Label> <asp:Label ID="Label4" runat="server" Text=""></asp:Label> <asp:Label ID="Label5" runat="server" Text=""></asp:Label> <asp:Label ID="Label6" runat="server" Text=""></asp:Label> <asp:Label ID="Label7" runat="server" Text=""></asp:Label> </div> <br /><br /> <asp:Button ID="Button1" runat="server" Text="Delete All Cookies" onclick="Button1_Click" /> <br /><br /> </form> </body> </html>
ResultCookie.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Configuration;
using System.Data.SqlClient;
public partial class ResultCookis : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Cookies1"] != null)
{
string txtbox = Request.Cookies["Cookies1"].Value; //For First Way
Label1.Text = txtbox.ToString();
//for 2nd Cookie
string txtbox2 = Request.Cookies["Cookies2"].Value;
Label2.Text = txtbox2.ToString();
//For Multiple values in single cookie
Label3.Text = Request.Cookies["Cookies3"]["name"];
Label4.Text = Request.Cookies["Cookies3"]["class"];
Label5.Text = Request.Cookies["Cookies3"]["sec"];
Label6.Text = Request.Cookies["Cookies3"]["roll"];
}
else
{
string msg = "No Cookies";
Label7.Text = msg;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["Cookies1"] != null)
{
Response.Cookies["Cookies1"].Expires = DateTime.Now.AddHours(-1);
}
if (Request.Cookies["Cookies2"] != null)
{
Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(-1);
}
if (Request.Cookies["Cookies3"] != null)
{
Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(-1);
}
Response.Redirect("ResultCookis.aspx"); //to refresh the page
}
}
Happy Coding 🙂