Click here to Skip to main content
15,884,040 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.Configuration;
using System.Text.RegularExpressions;

namespace Ventaur.Web.Security.Configuration {

	/// <summary>
	/// Indicates the type of security for a file or directory.
	/// </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>
	/// Represents an file or directory entry in the &lt;secureWebPages&gt;
	/// configuration section.
	/// </summary>
	public abstract class SecureWebPageItemSetting : ConfigurationElement {

		#region Constructors

		/// <summary>
		/// Creates an instance of SecureWebPageItemSetting.
		/// </summary>
		public SecureWebPageItemSetting()
			: base() {
		}
	    
	    /// <summary>
	    /// Creates an instance with an initial path value.
	    /// </summary>
	    /// <param name="path">The relative path to the file.</param>
		public SecureWebPageItemSetting(string path)
	        : this() {
			Path = path;
	    }

		/// <summary>
	    /// Creates an instance with initial values.
	    /// </summary>
	    /// <param name="path">The relative path to the file.</param>
	    /// <param name="secure">The type of security for the file.</param>
		public SecureWebPageItemSetting(string path, SecurityType secure)
	        : this(path) {
			Secure = secure;
	    }

		#endregion

		#region Properties
		
		/// <summary>
		/// Gets or sets the path of this item.
		/// </summary>
		[ConfigurationProperty("path", IsRequired = true, IsKey = true)]
		public virtual string Path {
			get { return (string)this["path"]; }
			set { this["path"] = value; }
		}

		/// <summary>
		/// Gets or sets the type of security for this item.
		/// </summary>
		[ConfigurationProperty("secure", DefaultValue = SecurityType.Secure)]
		public SecurityType Secure {
			get { return (SecurityType)this["secure"]; }
			set { this["secure"] = value; }
		}

		#endregion

	}

	/// <summary>
	/// Represents a collection of SecureWebPageItemSetting objects.
	/// </summary>
	public abstract class SecureWebPageItemSettingCollection : ConfigurationElementCollection {

		/// <summary>
		/// Returns the index of a specified item in the collection.
		/// </summary>
		/// <param name="item">The item to find.</param>
		/// <returns>Returns the index of the item.</returns>
		public int IndexOf(SecureWebPageItemSetting item) {
			if (item != null)
				return BaseIndexOf(item);
			else
				return -1;
		}

		/// <summary>
		/// Returns the index of an item with the specified path in the collection.
		/// </summary>
		/// <param name="Path">The path of the item to find.</param>
		/// <returns>Returns the index of the item with the path.</returns>
		public int IndexOf(string path) {
			return this.IndexOf((SecureWebPageItemSetting)BaseGet(path.ToLower()));
		}
		
	}

}

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