Description of Cookie
Cookie object is used to store the sort amount of data onto client end. It executes on server and resides on client browser.
When any user request first time for any web application then the web server generate an unique session id that is farther send back to client in the form of cookies. This cookie is known as the session cookie or default cookie and the name of the cookie is asp.net_session id.
The default time for cookie existence on the browser when the browser will close cookie will expired (in case of session cookie).
Cookies are domain specific that is when the domain of web application will be change cookie will be change.
To store data in a cookie is not secure due to it's location at client end. Cookie was introduced with first version of Netscape navigator (that was 1.0).
There are two type of cookie:
-
Session cookie
-
Persistent cookie
Session cookie can not store on the hard disk and Persistent cookie can be store on the hard disk.
It can be found as-
C/: document and setting/user profile/cookie
We can create cookie manually using http cookie class.Asp.net supports the multi values cookie. Numbers of cookies are depending on the domain, space on hard disk.
If cookie less property is check in browser that is browser does not support cookies there server create cookie less server that is the session id is upended into browser URL.
To write a cookie by creating an instance of the Http Cookie object:
- Create an object of type Http Cookie and assign it a name.
- Assign values to cookie's sub keys and set any cookie properties.
- Add the cookie to the Cookies collection.
HttpCookie ck1, ck2;
protected void Button1_Click(object sender, EventArgs e)
{
ck1 = new HttpCookie("cookiename1", TextBox1.Text);
ck2 = new HttpCookie("cookiename2", TextBox2.Text);
Response.Cookies.Add(ck1);
Response.Cookies.Add(ck2);
}
Here we are creating two cookies and storing the information in those cookies and we are accesing the values of those both cookies on another page.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
HttpCookie ck1,ck2;// Here we are creating the instance of cookie
protected void Button1_Click(object sender, EventArgs e)
{
ck1 = new HttpCookie("xx",TextBox1.Text); // here we are storing the value in a cookie
ck2 = new HttpCookie("yy",TextBox2.Text);
Response.Cookies.Add(ck1);
Response.Cookies.Add(ck2);
Response.Redirect("new webform.aspx");// redirect to another page
}
}
Second page coding to access the value of cookies from previous page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class new_webform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = Request.Cookies["xx"].Value;
// Accessing the values of cookies in a text box
TextBox2.Text = Request.Cookies["yy"].Value;
}
// To show the no. of cookies
protected void Button2_Click(object sender, EventArgs e)
{
foreach (string str in Request.Cookies)
{
Response.Write(str + "=" + Request.Cookies[str].Value + "<br>");
}
}
}
The following files are generete when cookie creates.
-
App_data
-
.aspx(asp.net server page)
-
.aspx.cs (visual c# source file)
-
Web.config(xml configuration file)
Authentication:
-
Cookies can be used to recognize authenticated users by a server This can be done for example as follows:User enters username and password in the text fields of a login page and sends them to the server.
- The server receives username and password and checks them; if correct, it sends back a page confirming that login has been successful together with a cookie, storing the pair user/cookie .
-
Every time the user requests a page from the server, the browser automatically sends the cookie back to the server; the server compares the cookie with the stored ones; if a match is found, the server knows which user has requested that
Why we use Cookie ?
-
To collect demographic information about who is visiting the Web site. Sites often use this information to track how often visitors come to the site and how long they remain on the site.
-
To monitor advertisements. Web sites will often use cookies to keep track of what ads it lets you see and how often you see ads.