Introduction to Cookies in ASP.Net

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

  1. HttpCookie Cookies1 = new HttpCookie("Cookies1");  
  2. Cookies1.Value = txt1.Text;  
  3. Response.Cookies.Add(Cookies1);  
2nd way
  1. Response.Cookies["Cookies1"].Value = txt1.Text;  

3rd way

We can write multiple values in each cookie.

  1. Response.Cookies["Cookies"]["Name"] = txt1.Text;  
  2. Response.Cookies["Cookies"]["class"] = txt2.Text; Response.Cookies["Cookies"]["sec"] = txt3.Text;  
  3. 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:

  1. String cooks=Request.Cookies[“Cookies”].Value;  
And the 3rd way for multiple values in a single cookie:
  1. String name=Request.Cookies["Cookies"]["Name"];  
  2. String class=Request.Cookies["Cookies"]["class"];  
  3. String sec=Request.Cookies["Cookies"]["sec"];  
  4. Int roll= String name=Request.Cookies["Cookies"]["roll"];  
Now how to Delete a Cookie
  1. 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:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CreateCookies.aspx.cs" Inherits="CreateCookies" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.     <b>Pleas enter your Name:</b>  
  13.     <br /><br />  
  14.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  15.     <br /><br />  
  16.         <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click1"/>  
  17.   
  18.     </div>  
  19.     </form>  
  20. </body>  
  21. </html>  

CreateCookie.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Threading;  
  7.   
  8. public partial class CreateCookies : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.   
  15.   
  16.     protected void Button1_Click1(object sender, EventArgs e)  
  17.     {  
  18.   
  19.         //First Way  
  20.         HttpCookie Cookies1 = new HttpCookie("Cookies1");  
  21.         Cookies1.Value = TextBox1.Text;  
  22.         Cookies1.Expires = DateTime.Now.AddHours(1);  
  23.         Response.Cookies.Add(Cookies1);  
  24.   
  25.   
  26.           
  27.       //  Second Way  
  28.         Response.Cookies["Cookies2"].Value = TextBox1.Text;  
  29.         Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(1);  
  30.           
  31.   
  32.           
  33.       //  Multiple values in single cookie  
  34.         Response.Cookies["Cookies3"]["name"] = "AAFIA";  
  35.         Response.Cookies["Cookies3"]["class"] = "class-ix";  
  36.         Response.Cookies["Cookies3"]["sec"] = "A";  
  37.         Response.Cookies["Cookies3"]["roll"] = "4";  
  38.           
  39.         Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(1);  
  40.           
  41.   
  42.         Response.Redirect("ResultCookis.aspx");  
  43.     }  
  44. }  
Now we access cookies on another page called ResultCookie.aspx as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultCookis.aspx.cs" Inherits="ResultCookis" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.    <div>  
  12.         <b>Now See Cookies Data Here: </b>  
  13.         <br /><br />  
  14.        first way <br />  
  15.        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>  <br />     
  16.        second way <br />  
  17.        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>  <br />  
  18.        multiple Value in  single Cookies <br />  
  19.        <asp:Label ID="Label3" runat="server" Text=""></asp:Label>          
  20.        <asp:Label ID="Label4" runat="server" Text=""></asp:Label>       
  21.        <asp:Label ID="Label5" runat="server" Text=""></asp:Label>       
  22.        <asp:Label ID="Label6" runat="server" Text=""></asp:Label>       
  23.        <asp:Label ID="Label7" runat="server" Text=""></asp:Label>   
  24.     </div>  
  25.     <br /><br />  
  26.     <asp:Button ID="Button1" runat="server" Text="Delete All Cookies"   
  27.         onclick="Button1_Click" />  
  28.     <br /><br />  
  29.     </form>  
  30. </body>  
  31. </html>  
ResultCookie.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Data;  
  7. using System.Web.Configuration;  
  8. using System.Data.SqlClient;  
  9.   
  10.   
  11. public partial class ResultCookis : System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         if (Request.Cookies["Cookies1"] != null)  
  16.         {  
  17.   
  18.             string txtbox = Request.Cookies["Cookies1"].Value; //For First Way  
  19.             Label1.Text = txtbox.ToString();  
  20.   
  21.             //for 2nd Cookie  
  22.   
  23.             string txtbox2 = Request.Cookies["Cookies2"].Value;  
  24.             Label2.Text = txtbox2.ToString();  
  25.               
  26.             //For Multiple values in single cookie  
  27.   
  28.   
  29.             Label3.Text = Request.Cookies["Cookies3"]["name"];  
  30.             Label4.Text = Request.Cookies["Cookies3"]["class"];  
  31.             Label5.Text = Request.Cookies["Cookies3"]["sec"];  
  32.             Label6.Text = Request.Cookies["Cookies3"]["roll"];  
  33.   
  34.         }  
  35.         else  
  36.         {  
  37.             string msg = "No Cookies";  
  38.             Label7.Text = msg;  
  39.         }  
  40.     }  
  41.   
  42.     protected void Button1_Click(object sender, EventArgs e)  
  43.     {  
  44.         if (Request.Cookies["Cookies1"] != null)  
  45.         {  
  46.             Response.Cookies["Cookies1"].Expires = DateTime.Now.AddHours(-1);  
  47.             
  48.         }  
  49.         if (Request.Cookies["Cookies2"] != null)  
  50.         {  
  51.             Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(-1);  
  52.             
  53.         }  
  54.         if (Request.Cookies["Cookies3"] != null)  
  55.         {  
  56.             Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(-1);  
  57.             
  58.         }  
  59.         Response.Redirect("ResultCookis.aspx");  //to refresh the page  
  60.     }  
  61. }  
Output screen



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:



Similar Articles