Javatpoint Logo
Javatpoint Logo

ASP.NET Cookie

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.

  • Cookies collection
  • HttpCookie

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.


HttpCookie Example

In the following example, we are creating and adding cookie with the help of HttpCookie class.

// CookieExample.aspx

Code

// CookieExample.aspx.cs

Cookie Collection Example

In the following example, we are adding cookie directly to the Cookies collection.

// Default.aspx

CodeBehind

// Default.aspx.cs

Output:

This example will store selected values as cookie.

ASP Net Cookie 1
ASP Net Cookie 2
Next TopicASP.NET Session





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA