Cookies in ASP.Net



ASP.Net Cookie Example

Cookies is a 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.

Cookies stored user computer at “C”\Document and Setting\Current login_User\Cookie”.

Types of Cookies

  1. Persistence Cookie
  2. Non-Persistence Cookie

1. Persistence Cookie

This types of cookies are permanently stored on user hard drive.
Cookies which have an expiry date time are called persistence cookes. This types of cookies stored user hard drive permenently till the date time we set.

Example to create persistence cookie

Response.Cookies[“name”].Value = “Meera Academy”;
Response.Cookies[“MeeraAcademy”].Expires = DateTime.Now.AddMinutes(10);

we can also create same cookies as like below

HttpCookie strname = new HttpCookie(“name”);
strname.Value = “Meera Academy”;
strname.Expires = DateTime.Now.AddMinutes(10);
Response.Cookies.Add(strname);

In above code we use Response.Cookies object for create Cookie.
In above example we have set 10 Minute time for Expire Cookie, we can retrieve cookie values up to 10 minutes, after 10 minutes the cookies automatically expires.

2. Non-Persistence Cookie

This types of cookies are not permanently stored on user hard drive. It stores the information up the user accesng the same browser. When user close the browser the cookies will be automatically deleted.

Example to create non-persistnce cookie

Response.Cookies[“name”].Value = “Meera Academy”;

we can also create same non-persistence cookies as

HttpCookie strname = new HttpCookie(“name”);
strname.Value = “Meera Academy”;
Response.Cookies.Add(strname);

Read Cookie Information

if (Request.Cookies[“name”] != null)
{
Label1.Text = Request.Cookies[“name”].Value;
}

ASP.Net Cookie Example

Open visual studio and design web form as shows below figure for create cookie and retrieve cookie information.

Cookie Example in ASP.Net
Cookie Example in ASP.Net

C# code for Cookie Example

Create Cookie Button C# Code

protected void btncreate_Click(object sender, EventArgs e)
    {
       Response.Cookies["name"].Value = txtcreatecookie.Text;
       Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(1);
       Label1.Text = "Cookie Created";
       txtcreatecookie.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.

Retrieve Cookie Button Code

protected void btnretrieve_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["name"] == null)
        {
            txtretrieve.Text = "No cookie found";
        }
        else
        {
            txtretrieve.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”.

Related ASP.Net Topics :

Session State in C#.Net
Application State in C#.Net


Subscribe us

If you liked this asp.net post, then please subscribe to our YouTube Channel for more asp.net video tutorials.

We hope that this asp.net c# tutorial helped you to understand about Cookies in C#.


Next asp.net tutorial we will understand about Context Object in C#.


Leave a Reply

Your email address will not be published. Required fields are marked *