ASP.NET Cookie is a small bit of text that is used to store user-specific information. This information can be read by the web application whenever user visits the site.
When a user requests for a web page, web server sends not just a page, but also a cookie containing the date and time. This cookie stores in a folder on the user's hard disk.
When the user requests for the web page again, browser looks on the hard drive for the cookie associated with the web page. Browser stores separate cookie for each different sites user visited.
Note: The Cookie is limited to small size and can be used to store only 4 KB (4096 Bytes) text.
There are two ways to store cookies in ASP.NET application.
We can add Cookie either to Cookies collection or by creating instance of HttpCookie class. both work same except that HttpCookie require Cookie name as part of the constructor.
In the following example, we are creating and adding cookie with the help of HttpCookie class.
Cookie Collection Example
In the following example, we are adding cookie directly to the Cookies collection.
This example will store selected values as cookie.