Cookies In ASP.NET

Cookies

A cookie is a small text based program which stores the information on client machine. This information can be accessed later on when a user visits the same website and same browser. This information is stored on the clients browser for a temporary period. Cookies should not be used for storing confidential information like passwords.

There are 2 types of cookies:
  1. Session Cookie/Browser cookie:

    By default the cookie values are stored only in the browser once the browser is closed the cookie values are lost. 
    These cookies are stored only for temporary period and are called session cookies.

  2. Persistent Cookies:

    Persistent cookies are the cookies where the cookie values are stored on the local disk for a specific period
There are 2 methods for interacting with the cookies:
  1. Response & request cookies
  2. Http cookies
Response object cookies are used to send the cookie information from server to client browser. Request object cookies are used to read  the cookie information from client to server.

Http Cookie


It provides an object oriented approach for working with the cookies. It consists of a class called HttpCookie through which cookies are handled. 

Example
 
 
Cookies.aspx.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace WebApp7am  
  9. {  
  10.     public partial class cookies : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             if (IsPostBack == false)  
  15.             {  
  16.                 HttpCookie cookie;  
  17.                 cookie = Request.Cookies["usercookie"];  
  18.                 if (cookie != null)  
  19.                 {  
  20.                     TextBox1.Text = cookie.Value;  
  21.                 }  
  22.             }  
  23.   
  24.         }  
  25.   
  26.         protected void btnSignIn_Click(object sender, EventArgs e)  
  27.         {  
  28.             if(CheckBox1.Checked==true)  
  29.             {  
  30.                 HttpCookie cookie = new HttpCookie("usercookie");  
  31.                 cookie.Value = TextBox1.Text;  
  32.                 cookie.Expires = DateTime.Now.AddDays(2);  
  33.                 Response.Cookies.Add(cookie);  
  34.             }  
  35.             Response.Redirect("default.aspx");  
  36.         }  
  37.     }  
  38. }  
Note:

btnSignIn_click:

When a user clicks on the signin button a postback occurs and the click event checks the status of the checkbox. If the checkbox is checked then a cookie is created with the value entered in the textbox and an expiry is assigned for a period of 2 days. If the checkbox is  unchecked then no cookie is created.

Page_load:

Whenever the page is executed this event will get fired and it checks the status of the postback. If ispostback is false ie.the user has executed the page for the first time and the program will check the cookie in the client machine and if it exists reads the cookie into the textbox. This will not executed when the user clicks on the button (when the postback occurs).
 
View cookies in Google chrome 
  1. Go to Settings:

    enter image description here
         (nowyou are on chrome://settings/)

  2. Scroll to the Privacy section, then click Content Settings.

    enter image description here
                            (nowyou are on chrome://settings/content)

  3. Click on All cookies and site data.

    enter image description here
               (now you are on chrome://settings/cookies)
  4. Here you canview/remove cookie or remove all cookies. Click on the labels to see details.

    enter image description here

Up Next
    Ebook Download
    View all
    Learn
    View all