How To Use Cookies In Asp.Net

Using Cookies

One way to move information between pages is to use a Cookie object. It is a method that allows us to create a file called Cookie (Cookie file) on the client computer and write the information in it. In this way, we can read and use or change the information in this file every time the visitor visits our page.

The file to be created is called a filename. At the same time, the Expires feature is used to specify how long that file will be valid. After the specified date, the file will be deleted.

Information is stored in the cookie file in the form of parameters and values.

Advantages of Cookie Object:

  • We may store user-specific information on the user's computer.
  • Even days later, we can access this information.
  • For example, we can prevent the same user from voting more than once in applications such as surveys.
  • We can take advantage of them by keeping user-specific statistics in the cookie file.
  • We may share this information with other sites and applications. For example, this is why we see the products we look at on shopping sites as google ads while browsing other sites.

Disadvantages of Using Cookies:

  • It is weak in terms of security. Others can also read these files.
  • The browser used by the visitor may not allow cookies.
  • The visitor can delete the cookies himself.

Creating Cookie

There are two methods for creating a cookie file and writing data into it. However, processing will be done using the HttpCookie class.

HttpCookie myCookie = new HttpCookie("MyCookieFile");
myCookie ["UserName"] = TextBox1.Text;
myCookie ["Password"] = TextBox2.Text;
myCookie .Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(myCookie );


  • An object named myCookie has been created from the HttpCookie class.
  • It is stated that the name of the file to be created on the disk of the client computer will be MyCookieFile.
  • In this file, two parameters named UserName and Password have been created and their values have been assigned.
  • The validity date of the file is set as 10 days after the current date.
  • Finally, the file is created with the Add method.

Reading Cookie

Similarly, we can read the data in the cookie file:

HttpCookie myCookie = Request.Cookies["MyCookieFile"];
TextBox1.Text = myCookie["UserName"];
TextBox2.Text = myCookie["Password"];

If the cookie file is not found during the reading process, an error will occur. To avoid this, the following method can be used:

if (Request.Cookies["cerezDosyam"] != null)
HttpCookie myCookie = Request.Cookies["MyCookieFile"];
TextBox1.Text = myCookie ["UserName"];
TextBox2.Text = myCookie ["Password"];

Cookie Operations Using Response and Request Methods 

Without using the HttpCookie class, cookies can be written with the Response method and read with the Request method.

Writing a Cookie using Response method:

Response.Cookies["MyCookieFile"]["UserName"] = TextBox1.Text;
Response.Cookies["MyCookieFile"]["Password"] = "TextBox2.Text";
Response.Cookies["MyCookieFile"].Expires = DateTime.Now.AddDays(10); 

If only one information is to be added to the cookie, it can be done without using parameters as follows:

Response.Cookies["MyCookieFile"].Value = TextBox1.Text;

Reading Cookie with Request method:

TextBox1.Text = Request.Cookies["MyCookieFile"]["UserName"];
TextBox2.Text = Request.Cookies["MyCookieFile"]["Password"];

If there is only one information in the cookie, it can be read as:

Label1.Text = Request.Cookies["MyCookieFile"].Value;

Cookie Silme İşlemi

if (Request.Cookies["MyCookieFile"] != null)
HttpCookie myCookie = new HttpCookie("MyCookieFile");
myCookie .Expires = DateTime.Now.AddDays(-1d);
Response.Cookies.Add(myCookie );


Creating cookies in, writing c# cookies, reading and writing cookies, deleting cookies, disabling cookies, advantages and disadvantages of using cookies


There are no examples related to this subject.


Read 374 times.

Online Users: 983