This article provides an overview of working with cookies in ASP.NET applications. I will show you the mechanics of working with cookies in ASP.NET. Along the way, I will explain various features and (occasional) oddities of cookies and the support in ASP.NET for them.
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.
Imagine that when users request a page from your site, www.mindstick.com, your application sends not just a page, but a cookie containing the date and time. When the user's browser gets the page, the browser also gets the cookie, which it stores in a folder on the user's hard disk.
Example: below provided a source code for Cookie.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies.aspx.cs" Inherits="Session" %>
<form id="form1" runat="server">
<asp:Label ID="lblRecord" runat="server" /> <br />
<asp:Button ID="btnSetCookie" runat="server" Text="SetCookie" OnClick="btnSetCookie_Click" />
<asp:Button ID="btnPrintCookie" runat="server" Text="Print Cookie" OnClick="btnPrintCookie_Click" />
public partial class Cookies : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnSetCookie_Click(object sender, EventArgs e)
cook.Value = "MyCookie";
protected void btnPrintCookie_Click(object sender, EventArgs e)
lblRecord.Text = cook.Name;