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 tp declare a Cookie.
1st way
- HttpCookie Cookies1 = new HttpCookie("Cookies1");
- Cookies1.Value = txt1.Text;
- Response.Cookies.Add(Cookies1);
- 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;
- 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"];
- Response.Cookies["Cookies1"].Expires = DateTime.Now.AddHours(-1);
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");
- }
- }
- <%@ 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>
- 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
- }
- }

After clicking the submit button the page is rendered to the ResulCookis.aspx page and shows the cookie value as in the following:

Here after clicking on the delete all the cookies button, it deletes a cookie as in the following:

Bruno PétersonPosted Jun 24, 2015, 3:14 PM
Simple and good!
Santhakumar MunuswamyPosted May 10, 2015, 11:56 AM
Thanks for nice article
Nanhe SiddiquePosted May 9, 2015, 12:05 AM
thanks sibeesh and abhishek
Abhishek JaiswalPosted May 8, 2015, 12:34 PM
Good introduction! :)
Sibeesh VenuPosted May 8, 2015, 7:04 AM
Good one .