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

Transparent Cookie Encryption Via HTTP Module

Rate me:
Please Sign up or sign in to vote.
3.54/5 (7 votes)
5 Jul 2009CPOL4 min read 53.1K   384   23  
A C# HTTP module that encrypts and decrypts cookies transparently to an application
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 CookieEncryptionTestWeb
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class CookieEncryptionTester : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.Label Label1;

	
		private void Page_Load(object sender, System.EventArgs e)
		{
			DateTime Now = DateTime.Now;

			HttpCookie TestCookie1 = Request.Cookies["TestCookie1"];
			HttpCookie TestCookie2 = Request.Cookies["TestCookie2"];
			HttpCookie TestCookie3 = Request.Cookies["TestCookie3"];
			HttpCookie TestCookie4 = Request.Cookies["TestCookie4"];

			if(TestCookie1 != null) 
			{
				Label1.Text = "Read Cookie 1: " +  TestCookie1.Value;
			}
			else
			{
				TestCookie1 = new HttpCookie("TestCookie1", "Value of TestCookie1 @ " + Now.ToLongTimeString());
				Response.Cookies.Add(TestCookie1);
			}

			if(TestCookie2 != null)
			{
				Label2.Text = "Read Cookie 2: " +  TestCookie2.Value;
			}
			else
			{
				TestCookie2 = new HttpCookie("TestCookie2", "Value of TestCookie2 @ " + Now.ToLongTimeString());
				TestCookie2.Expires = Now + new TimeSpan(1, 0, 0);
				Response.Cookies.Add(TestCookie2);
			}

			if(TestCookie3 != null) 
			{
				Label3.Text = "Read Cookie 3: " +  TestCookie3.Value;
			}
			else
			{
				TestCookie3 = new HttpCookie("TestCookie3", "Value of TestCookie3 @ " + Now.ToLongTimeString());
				Response.Cookies.Add(TestCookie3);
			}

			if(TestCookie4 != null)
			{
				Label4.Text = "Read Cookie 4: " +  TestCookie4.Value;
			}
			else
			{
				TestCookie4 = new HttpCookie("TestCookie4", "Value of TestCookie4 @ " + Now.ToLongTimeString());
				TestCookie4.Expires = Now + new TimeSpan(5, 0, 0);
				Response.Cookies.Add(TestCookie4);
			}
		}

		#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.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions