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.
Dim ck1 As HttpCookie, ck2 As HttpCookie
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
ck1 = New HttpCookie("cookiename1", TextBox1.Text)
ck2 = New HttpCookie("cookiename2", TextBox2.Text)
Response.Cookies.Add(ck1)
Response.Cookies.Add(ck2)
End Sub
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.
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Dim ck1 As HttpCookie, ck2 ' Here we are creating the instance of cookie As HttpCookie
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
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
End Sub
End Class
Second page coding to access the value of cookies from previous page
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Partial Public Class New_webform
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = Request.Cookies("xx").Value
' Accessing the values of cookies in a text box
TextBox2.Text = Request.Cookies("yy").Value
End Sub
' To show the no. of cookies
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim str As String
For Each str In Request.Cookies
Response.Write(str + "=" + Request.Cookies(str).Value + "<br>")
Next
End Sub
End Class
The following files are generete when cookie creates.
-
App_data
-
.aspx(asp.net server page)
-
.aspx.vb (visual Basic.NET 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.