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:
- 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.
- 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:
- Response & request cookies
- 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- namespace WebApp7am
- {
- public partial class cookies : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack == false)
- {
- HttpCookie cookie;
- cookie = Request.Cookies["usercookie"];
- if (cookie != null)
- {
- TextBox1.Text = cookie.Value;
- }
- }
-
- }
-
- protected void btnSignIn_Click(object sender, EventArgs e)
- {
- if(CheckBox1.Checked==true)
- {
- HttpCookie cookie = new HttpCookie("usercookie");
- cookie.Value = TextBox1.Text;
- cookie.Expires = DateTime.Now.AddDays(2);
- Response.Cookies.Add(cookie);
- }
- Response.Redirect("default.aspx");
- }
- }
- }
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
- Go to Settings:
(nowyou are on chrome://settings/)
- Scroll to the Privacy section, then click Content Settings.
(nowyou are on chrome://settings/content)
- Click on All cookies and site data.
(now you are on chrome://settings/cookies)
Here you canview/remove cookie or remove all cookies. Click on the labels to see details.