In this article I will explain with an example, how to use Browser
Cookies in ASP.Net i.e. reading values stored in
Cookies, writing (saving) values in
Cookies and also how to remove (delete)
Cookies in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of following controls.
TextBox – For capturing user input.
Button – For
Write,
Read and
Delete the
Cookies.
Name:
<asp:TextBox ID="txtName" runat="server" />
<br />
<br />
<asp:Button ID="btnWrite" Text="Write Cookie" runat="server" OnClick="WriteCookie" />
<asp:Button ID="btnRead" Text="Read Cookie" runat="server" OnClick="ReadCookie" />
<asp:Button ID="btnDelete" Text="Remove Cookie" runat="server" OnClick="RemoveCookie" />
Writing Cookies
When the Write Cookie Button is clicked, the WriteCookie event handler is executed.
Inside the WriteCookie event handler, the value of the Name TextBox is saved to the Browser Cookie using the object of the HttpCookie class.
Finally, the Cookie is added to the Response.Cookies collection.
C#
protected void WriteCookie(object sender, EventArgs e)
{
//Create a Cookie with a suitable Key.
HttpCookie nameCookie = new HttpCookie("Name");
//Set the Cookie value.
nameCookie.Values["Name"] = txtName.Text;
//Set the Expiry date.
nameCookie.Expires = DateTime.Now.AddDays(30);
//Add the Cookie to Browser.
Response.Cookies.Add(nameCookie);
}
VB.Net
Protected Sub WriteCookie(sender As Object, e As EventArgs)
'Create a Cookie with a suitable Key.
Dim nameCookie As New HttpCookie("Name")
'Set the Cookie value.
nameCookie.Values("Name") = txtName.Text
'Set the Expiry date.
nameCookie.Expires = DateTime.Now.AddDays(30)
'Add the Cookie to Browser.
Response.Cookies.Add(nameCookie)
End Sub
Reading Cookies
When the Read Cookie Button is clicked, the ReadCookie event handler is executed.
Inside the ReadCookie event handler, the HttpCookie object is fetched from the Request.Cookies collection using its Key.
Finally, the value read from the Cookie is displayed using
JavaScript Alert Message Box.
C#
protected void ReadCookie(object sender,EventArgs e)
{
//Fetch the Cookie using its Key.
HttpCookie nameCookie = Request.Cookies["Name"];
//If Cookie exists fetch its value.
string name = nameCookie != null ? nameCookie.Value.Split('=')[1] : "undefined";
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + name + "');", true);
}
VB.Net
Protected Sub ReadCookie(sender As Object, e As EventArgs)
'Fetch the Cookie using its Key.
Dim nameCookie As HttpCookie = Request.Cookies("Name")
'If Cookie exists fetch its value.
Dim name As String = If(nameCookie IsNot Nothing, nameCookie.Value.Split("="c)(1), "undefined")
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & name & "');", True)
End Sub
Removing Cookies
When the Remove Cookie Button is clicked, the RemoveCookie event handler is executed.
Inside the RemoveCookie event handler, the Cookie object is fetched from the Request.Cookies collection using its Key.
A Cookie cannot be removed or deleted, it only can be made expired and hence, the Expiry Date of the Cookie is set to a past date and the Cookie is updated back into the Response.Cookies collection.
C#
protected void RemoveCookie(object sender, EventArgs e)
{
//Fetch the Cookie using its Key.
HttpCookie nameCookie = Request.Cookies["Name"];
//Set the Expiry date to past date.
nameCookie.Expires = DateTime.Now.AddDays(-1);
//Update the Cookie in Browser.
Response.Cookies.Add(nameCookie);
}
VB.Net
Protected Sub RemoveCookie(sender As Object, e As EventArgs)
'Fetch the Cookie using its Key.
Dim nameCookie As HttpCookie = Request.Cookies("Name")
'Set the Expiry date to past date.
nameCookie.Expires = DateTime.Now.AddDays(-1)
'Update the Cookie in Browser.
Response.Cookies.Add(nameCookie)
End Sub
Screenshot
Demo
Downloads