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

Switching Between HTTP and HTTPS Automatically: Version 2

Rate me:
Please Sign up or sign in to vote.
4.91/5 (223 votes)
7 Feb 2011CPOL18 min read 3.5M   680  
An article on automatically switching between HTTP and HTTPS protocols without hard-coding absolute URLs
using System;
using System.Collections;

namespace Hyper.Web.Security
{

	/// <summary>
	/// Indicates the type of security for a SecureWebPageItem.
	/// </summary>
	public enum SecurityType
	{
		/// <summary>
		/// The item should be secure.
		/// </summary>
		Secure,

		/// <summary>
		/// The item should be insecure.
		/// </summary>
		Insecure,

		/// <summary>
		/// The item should be ignored.
		/// </summary>
		Ignore
	}


	/// <summary>
	/// The SecureWebPageItemComparer class implements the IComparer interface to compare.
	/// </summary>
	public class SecureWebPageItemComparer : IComparer
	{
		/// <summary>
		/// Initialize an instance of this class.
		/// </summary>
		public SecureWebPageItemComparer()
		{
		}

		/// <summary>
		/// Compares the two objects as string and SecureWebPageItem or both SecureWebPageItem 
		/// by the Path property.
		/// </summary>
		/// <param name="x">First object to compare.</param>
		/// <param name="y">Second object to compare.</param>
		/// <returns></returns>
		public int Compare(object x, object y)
		{
			// Check the type of the parameters
			if (!(x is SecureWebPageItem) && !(x is string))
				// Throw an exception for the first argument
				throw new ArgumentException("Parameter must be a SecureWebPageItem or a String.", "x");
			else if (!(y is SecureWebPageItem) && !(y is string))
				// Throw an exception for the second argument
				throw new ArgumentException("Parameter must be a SecureWebPageItem or a String.", "y");

			// Initialize the path variables
			string xPath = string.Empty;
			string yPath = string.Empty;

			// Get the path for x
			if (x is SecureWebPageItem)
				xPath = ((SecureWebPageItem) x).Path;
			else
				xPath = (string) x;

			// Get the path for y
			if (y is SecureWebPageItem)
				yPath = ((SecureWebPageItem) y).Path;
			else
				yPath = (string) y;

			// Compare the paths, ignoring case
			return string.Compare(xPath, yPath, true);
		}
	}


	/// <summary>
	/// The SecureWebPageItem class is the base class that represents entries in the &lt;secureWebPages&gt;
	/// configuration section.
	/// </summary>
	public class SecureWebPageItem
	{
		// Fields
		private SecurityType secure = SecurityType.Secure;
		private string path = string.Empty;

		/// <summary>
		/// Gets or sets the type of security for this directory or file.
		/// </summary>
		public SecurityType Secure
		{
			get { return secure; }
			set { secure = value; }
		}

		/// <summary>
		/// Gets or sets the path of this directory or file.
		/// </summary>
		public string Path
		{
			get { return path; }
			set { path = value; }
		}

		/// <summary>
		/// Creates an instance of this class.
		/// </summary>
		public SecureWebPageItem()
		{
		}

		/// <summary>
		/// Creates an instance with initial values.
		/// </summary>
		/// <param name="path">The relative path to the directory or file.</param>
		/// <param name="ignore">A flag to ignore security for the directory or file.</param>
		public SecureWebPageItem(string path, SecurityType secure)
		{
			// Initialize the path and secure properties
			this.path = Path;
			this.secure = secure;
		}

		/// <summary>
		/// Creates an instance with an initial path value.
		/// </summary>
		/// <param name="path">The relative path to the directory or file.</param>
		public SecureWebPageItem(string path) : this(path, SecurityType.Secure)
		{
		}

	}
}

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 States United States
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.

The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).

Comments and Discussions