Click here to Skip to main content
15,885,985 members
Articles / Web Development / ASP.NET

On The Care and Handling of Cookies

Rate me:
Please Sign up or sign in to vote.
4.87/5 (63 votes)
3 Jan 200313 min read 606.9K   2.1K   176  
Everything you ever wanted to know about ASP.NET Cookies but were too afraid to ask.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace InteractiveAspNet
{
	/// <summary>
	/// Summary description for AspNetCookies.
	/// </summary>
	public class AspNetCookies : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label myRequestCookie;
		protected System.Web.UI.WebControls.Button btnSetCookie;
		protected System.Web.UI.WebControls.Button btnClearCookie;
		protected System.Web.UI.WebControls.Button btnDoNothing;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (Request.Cookies["TestCookie"] != null)
				Response.Cookies.Set(Request.Cookies["TestCookie"]);
			else
				Response.Cookies.Set(new HttpCookie("TestCookie", ""));

			Response.Cookies["TestCookie"].Expires = DateTime.Now.AddYears(30);

			// Display the Request cookie on the page
			if (Response.Cookies["TestCookie"].Value == "")
				myRequestCookie.Text = "Cookie is null";
			else
				myRequestCookie.Text = Request.Cookies["TestCookie"].Value;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSetCookie.Click += new System.EventHandler(this.btnSetCookie_Click);
			this.btnClearCookie.Click += new System.EventHandler(this.btnClearCookie_Click);
			this.btnDoNothing.Click += new System.EventHandler(this.btnDoNothing_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected override void OnPreRender(System.EventArgs e)
		{
			if (Response.Cookies["TestCookie"].Value == Request.Cookies["TestCookie"].Value)
				Response.Cookies.Remove("TestCookie");

			base.OnPreRender(e);
		}

		private void btnSetCookie_Click(object sender, System.EventArgs e)
		{
			// Set up a cookie and redirect to this page to pick it up for display
			Response.Cookies["TestCookie"].Value = "Cookie is set";
			Response.Redirect("AspNetCookies.aspx");
		}

		private void btnClearCookie_Click(object sender, System.EventArgs e)
		{
			// Expire the cookie and redirect to this page to display a message
			Response.Cookies["TestCookie"].Expires = DateTime.Now.AddYears(-30);
			Response.Redirect("AspNetCookies.aspx");
		}

		private void btnDoNothing_Click(object sender, System.EventArgs e)
		{
			// Do absolutely nothing except redirect to simulate moving to another page
			Response.Redirect("AspNetCookies.aspx");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Paul lives in the heart of En a backwater village in the middle of England. Since writing his first Hello World on an Oric 1 in 1980, Paul has become a programming addict, got married and lost most of his hair (these events may or may not be related in any number of ways).

Since writing the above, Paul got divorced and moved to London. His hair never grew back.

Paul's ambition in life is to be the scary old guy whose house kids dare not approach except at halloween.

Comments and Discussions