How to read cookies in ASP.NET?
Posted by Sheo Narayan
on 6/16/2011 for Advance level | Points: 250
| Views : 29410
If you found plagiarised (copied)
or inappropriate content,
please let us know
the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
In this article, we are going to learn how to read cookies in different ways using ASP.NET.
A Cookie is a small amount of text that attached to the requet and response in between browser and the server. This small amount of text can be read by the application whenever user browse the it. Please note that cookie is not a good medium to store the confidential data as it is stored into the user browser.
<asp:Button ID="btnCreate" runat="server" Text="Read Cookie - One way" OnClick="ReadCookie1" />
<asp:Button ID="Button1" runat="server" Text="ReadCookie - Other way" OnClick="ReadCookie2" />
In the above code snippet, we have two buttons. The first button executes ReadCookie1 method and second button executes ReadCookie2 method.
protected void ReadCookie1(object sender, EventArgs e)
HttpCookie cookie = Request.Cookies["CookieKey1"];
protected void ReadCookie2(object sender, EventArgs e)
Get hundreds of ASP.NET How to Tips and Tricks.
In this method, we are retrieving the cookie using Request object and saving into HttpCookie variable. The next line writes the cookie value on the page.
In this method, we are retrieving the cookie value using Request.Cookies collection by passing the Cookiename as parameter.
In this article, we learnt how to read the cookies. To know how to set the expiration date time of the cookies, click here.
To learn how to create cookies in ASP.NET, click here.
Thanks. If you like this article, share it to your friends by tweeting or sharing it via facebook, linkedin or other websites.
Found interesting? Add this to: