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.