Updated on Kisan Patel
This tutorial will explain you two different ways of reading cookie in ASP.NET website.
Default.aspx
<asp:Button ID="btnRead1" runat="server" Text="Read Cookie - 1" OnClick="ReadCookie1" />
Default.aspx.cs
protected void ReadCookie1(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["CookieKey1"]; Response.Write(cookie.Value); }
In method 1, we are retrieving the cookie using Request
object and saving intoHttpCookie
variable. The next line writes the cookie value on the page.
Default.aspx
<asp:Button ID="btnRead2" runat="server" Text="ReadCookie - 2"
OnClick="ReadCookie2" />
Default.aspx.cs
protected void ReadCookie2(object sender, EventArgs e) { Response.Write(Request.Cookies["CookieKey2"].Value); }
In method 2, we are retrieving the cookie value using Request.Cookies
collection by passing the Cookiename as parameter.
Demo