Introduction
In this article I'll explain cookies in Asp.Net and how to use cookies in C# and VB.Net with an appropriate example. A way to use Browser Cookies in ASP.Net is reading values and keeping it in Cookies, writing (saving) values in Cookies and additionally a way to delete/remove/clear Cookies in ASP.Net using C# and VB.Net.
What is Cookie in Asp.Net ?
A cookie could be a tiny little bit of text that accompanies requests and pages as they're going between the online server and browser. The cookie contains data the online application will scan whenever the user visits a website.
For example, if a user requests a page from your web site and your application sends not simply a page, however additionally a cookie containing the date and time, once the user's browser gets the page, the browser additionally gets the cookie, that it stores in an exceedingly folder on the user's magnetic disk.
Later, if user requests a page from your web site once more, when the user enters the universal resource locator (URL) the browser appearance on the native magnetic disc for a cookie related to the universal resource locator (URL) . If the cookie exists, the browser sends the cookie to your web site together with the page request. Your application will then confirm the date and time that the user last visited the positioning. you may use the data to show a message to the user or check an expiration date.
Cookies are related to an internet website, not with a particular page, therefore the browser and server can exchange cookie data despite what page the user requests from your website. because the user visits completely different sites, every website would possibly send a cookie to the user's browser as well; the browser stores all the cookies on an individual basis.
How to Read, Write and Clear Cookies in ASP.Net ?
The following markup language consists of associate ASP.Net TextBox and 3 ASP.Net Buttons for Writing, Reading and Deleting browser cookies.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title></title>
- <style type="text/css">
- body
- {
- font-family: Arial;
- font-size: 10pt;
- }
- .button {
- background-color: #ff6a00;
- border: none;
- color: white;
- padding: 15px 32px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- }
- input[type=text] {
- border: none;
- width :33.5%;
- height:35px;
- font-size: 16px;
- border-bottom: 2px solid #0072ff;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <b> Name:</b>
- <asp:TextBox ID="txtName" runat="server" />
- <br />
- <br />
- <asp:Button ID="btnWrite" CssClass="button" Text="Write Cookie" runat="server" OnClick="WriteCookie" />
- <asp:Button ID="btnRead" CssClass="button" Text="Read Cookie" runat="server" OnClick="ReadCookie" />
- <asp:Button ID="btnDelete" CssClass="button" Text="Remove Cookie" runat="server" OnClick="RemoveCookie" />
- </form>
- </body>
- </html>
How to Write Cookies in ASP.Net ?
When the Write Cookie Button is clicked, the individual Button click event handler is executed that saves the worth of the Name TextBox to the Browser Cookie using the item of the HttpCookie category.
Finally the Cookie is added to the Response.Cookies assortment.
Write following code in click event of button WriteCookie.
C# Code
- protected void WriteCookie(object sender, EventArgs e)
- {
-
- HttpCookie nameCookie = new HttpCookie("Name");
-
-
- nameCookie.Values["Name"] = txtName.Text;
-
-
- nameCookie.Expires = DateTime.Now.AddDays(30);
-
-
- Response.Cookies.Add(nameCookie);
- }
Vb.Net Code
- Protected Sub WriteCookie(sender As Object, e As EventArgs)
-
- Dim nameCookie As New HttpCookie("Name")
-
-
- nameCookie.Values("Name") = txtName.Text
-
-
- nameCookie.Expires = DateTime.Now.AddDays(30)
-
-
- Response.Cookies.Add(nameCookie)
- End Sub
How to Read Cookies in Asp.Net ?
When you will click on read Cookie Button, the several Button event handler fetches the HttpCookie object from the Request.Cookies assortment using its Key.
The value read from the Cookie is displayed using JavaScript Alert Box.
Write the following code in click event of button ReadCookie.
C# Code
- protected void ReadCookie(object sender, EventArgs e)
- {
-
- HttpCookie nameCookie = Request.Cookies["Name"];
-
-
- string name = nameCookie != null ? nameCookie.Value.Split('=')[1] : "undefined";
- ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + name + "');", true);
- }
VB.Net Code
- Protected Sub ReadCookie(sender As Object, e As EventArgs)
-
- Dim nameCookie As HttpCookie = Request.Cookies("Name")
-
-
- Dim name As String = If(nameCookie IsNot Nothing, nameCookie.Value.Split("="c)(1), "undefined")
- ClientScript.RegisterStartupScript(Me.[GetType](), "alert", (Convert.ToString("alert('") & name) + "');", True)
- End Sub
How to Clear Cookies in Asp.Net ?
When the remove Cookie Button is clicked, the individual Button event handler fetches the Cookie object from the Request.Cookies assortment using its Key.
A Cookie can't be removed or deleted, it solely is created invalid and therefore the expiration Date of the Cookie is about a past date and therefore the Cookie is updated back to the Response.Cookies assortment.
Write the following code in click event of button RemoveCookie.
C# Code
- protected void RemoveCookie(object sender, EventArgs e)
- {
-
- HttpCookie nameCookie = Request.Cookies["Name"];
-
-
- nameCookie.Expires = DateTime.Now.AddDays(-1);
-
-
- Response.Cookies.Add(nameCookie);
- }
Vb.Net Code
- Protected Sub RemoveCookie(sender As Object, e As EventArgs)
-
- Dim nameCookie As HttpCookie = Request.Cookies("Name")
-
-
- nameCookie.Expires = DateTime.Now.AddDays(-1)
-
-
- Response.Cookies.Add(nameCookie)
- End Sub
Output Screen
Screen For Writing Cookie
Screen For Reading Cookie
Finally, Remove Cookie will remove the Cookie from your web browser.