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. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <b>Pleas enter your Name:</b>
  11. <br /><br />
  12. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  13. <br /><br />
  14. <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click1"/>
  15. </div>
  16. </form>
  17. </body>
  18. </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. public partial class CreateCookies : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. protected void Button1_Click1(object sender, EventArgs e)
  13. {
  14. //First Way
  15. HttpCookie Cookies1 = new HttpCookie("Cookies1");
  16. Cookies1.Value = TextBox1.Text;
  17. Cookies1.Expires = DateTime.Now.AddHours(1);
  18. Response.Cookies.Add(Cookies1);
  19. // Second Way
  20. Response.Cookies["Cookies2"].Value = TextBox1.Text;
  21. Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(1);
  22. // Multiple values in single cookie
  23. Response.Cookies["Cookies3"]["name"] = "AAFIA";
  24. Response.Cookies["Cookies3"]["class"] = "class-ix";
  25. Response.Cookies["Cookies3"]["sec"] = "A";
  26. Response.Cookies["Cookies3"]["roll"] = "4";
  27. Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(1);
  28. Response.Redirect("ResultCookis.aspx");
  29. }
  30. }
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. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <b>Now See Cookies Data Here: </b>
  11. <br /><br />
  12. first way <br />
  13. <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <br />
  14. second way <br />
  15. <asp:Label ID="Label2" runat="server" Text=""></asp:Label> <br />
  16. multiple Value in single Cookies <br />
  17. <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
  18. <asp:Label ID="Label4" runat="server" Text=""></asp:Label>
  19. <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
  20. <asp:Label ID="Label6" runat="server" Text=""></asp:Label>
  21. <asp:Label ID="Label7" runat="server" Text=""></asp:Label>
  22. </div>
  23. <br /><br />
  24. <asp:Button ID="Button1" runat="server" Text="Delete All Cookies"
  25. onclick="Button1_Click" />
  26. <br /><br />
  27. </form>
  28. </body>
  29. </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. public partial class ResultCookis : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. if (Request.Cookies["Cookies1"] != null)
  14. {
  15. string txtbox = Request.Cookies["Cookies1"].Value; //For First Way
  16. Label1.Text = txtbox.ToString();
  17. //for 2nd Cookie
  18. string txtbox2 = Request.Cookies["Cookies2"].Value;
  19. Label2.Text = txtbox2.ToString();
  20. //For Multiple values in single cookie
  21. Label3.Text = Request.Cookies["Cookies3"]["name"];
  22. Label4.Text = Request.Cookies["Cookies3"]["class"];
  23. Label5.Text = Request.Cookies["Cookies3"]["sec"];
  24. Label6.Text = Request.Cookies["Cookies3"]["roll"];
  25. }
  26. else
  27. {
  28. string msg = "No Cookies";
  29. Label7.Text = msg;
  30. }
  31. }
  32. protected void Button1_Click(object sender, EventArgs e)
  33. {
  34. if (Request.Cookies["Cookies1"] != null)
  35. {
  36. Response.Cookies["Cookies1"].Expires = DateTime.Now.AddHours(-1);
  37. }
  38. if (Request.Cookies["Cookies2"] != null)
  39. {
  40. Response.Cookies["Cookies2"].Expires = DateTime.Now.AddHours(-1);
  41. }
  42. if (Request.Cookies["Cookies3"] != null)
  43. {
  44. Response.Cookies["Cookies3"].Expires = DateTime.Now.AddDays(-1);
  45. }
  46. Response.Redirect("ResultCookis.aspx"); //to refresh the page
  47. }
  48. }
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: