0% found this document useful (0 votes)
6 views2 pages

Cookies

Uploaded by

Nadeem Makwana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Cookies

Uploaded by

Nadeem Makwana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ASP.

Net Cookie Example


Cookie is small pieces of text information which is stored on user hard drive using users
browser for identify users. It may contain username, ID, password or any information.
Cookie does not use server memory.

Types of Cookies

1. Persistence Cookie
These types of cookies are permanently stored on user hard drive.
Cookies which have an expiry date time are called persistence cookies. These types of
cookies stored user hard drive permanently till the date time we set.

protected void Button1_Click(object sender, EventArgs e)


{
Response.Cookies["name"].Value = TextBox1.Text;
Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
Label1.Text = "Cookie Created";
TextBox1.Text = "";

Here, we create cookie with name parameter and assign textbox values to name cookie
and also set expiry time 1 minute. The cookie destroyed after 1 minute.

protected void Button2_Click(object sender, EventArgs e)


{
if (Request.Cookies["name"] == null)
{
TextBox2.Text = "No cookie found";
}
else
{
TextBox2.Text = Request.Cookies["name"].Value;
}

On retrieve cookie button checks if cookie value not null then display cookie value in
result, but after 1 minute the cookie expires, after 1 minute cookie value will be null and
result will be “No cookie found”.

By: Asst. Prof. Khyati Raval Page 1


2. Non-Persistence Cookie
These types of cookies are not permanently stored on user hard drive. It stores the
information up the user accessing the same browser. When user closes the browser the
cookies will be automatically deleted.

Example to create non-persistence cookie:

protected void Button1_Click(object sender, EventArgs e)


{
Response.Cookies["name"].Value = TextBox1.Text;
Label1.Text = "Cookie Created";
TextBox1.Text = "";

}
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["name"] == null)
{
TextBox2.Text = "No cookie found";
}
else
{
TextBox2.Text = Request.Cookies["name"].Value;
}

By: Asst. Prof. Khyati Raval Page 2

You might also like