This tutorial will show how we can both write to and read from a simple cookie. C# version.

Using Cookies in web pages is very useful for temporarily storing small amounts of data, for the website to use. These Cookies are small text files that are stored on the user’s computer, which the web site can read for information; a web site can also write new cookies.

An example of using cookies efficiently would be for a web site to tell if a user has already logged in. The login information can be stored in a cookie on the user’s computer and read at any time by the web site to see if the user is currently logged in. This enables the web site to display information based upon the user’s current status – logged in or logged out.

Cookies can be very powerful, yet are very small text files and, fortunately, ASP.NET makes it easy for us to both read and write cookies.

In this example, we will have two ASPX pages: One to write a cookie, and one to read this same cookie.

The first ASPX page will look something like this:

The code-behind for this page will look something like this:

We also create a page to read the cookie, which will be as follows:

The code-behind for this page will look something like this:

Download Source Files