Code: Reading a Cookie (Visual C#)
The following example uses the HttpCookie class and its properties to read a cookie with a specific name.
HttpCookie myCookie = new HttpCookie("MyTestCookie"); myCookie = Request.Cookies["MyTestCookie"]; // Read the cookie information and display it. if (myCookie != null) Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value); else Response.Write("not found");
Compiling the Code
Start a new ASP.NET Web application and paste the code into the Page_Load method.
Replace the name
"MyTestCookie" with the name of the cookie to be read.
Values returned from a cookie are always of type string.
The browser can only send the data back to the server that originally created the cookie. However, malicious users can access cookies and read their contents.