Click here to Skip to main content
15,881,882 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>
	/// The different modes supported for the &lt;secureWebPages&gt; configuration section.
	/// </summary>
	public enum SecureWebPageMode {
		/// <summary>
		/// Indicates that web page security is on and all requests should be monitored.
		/// </summary>
		On,

		/// <summary>
		/// Only remote requests are to be monitored.
		/// </summary>
		RemoteOnly,

		/// <summary>
		/// Only local requests are to be monitored.
		/// </summary>
		LocalOnly,

		/// <summary>
		/// Web page security is off and no monitoring should occur.
		/// </summary>
		Off
	}

	/// <summary>
	/// The different modes for bypassing security warnings.
	/// </summary>
	public enum SecurityWarningBypassMode {
		/// <summary>
		/// Always bypass security warnings when switching to an unencrypted page.
		/// </summary>
		AlwaysBypass,

		/// <summary>
		/// Only bypass security warnings when switching to an unencrypted page if the proper query parameter is present.
		/// </summary>
		BypassWithQueryParam,

		/// <summary>
		/// Never bypass security warnings when switching to an unencrypted page.
		/// </summary>
		NeverBypass
	}


	/// <summary>
	/// Contains the settings of a secureWebPages configuration section.
	/// </summary>
	public class SecureWebPageSettings : ConfigurationSection {

		/// <summary>
		/// Creates an instance of SecureWebPageSettings.
		/// </summary>
		public SecureWebPageSettings()
			: base() {
		}

		#region Properties

		/// <summary>
		/// Gets or sets the name of the query parameter that will indicate to the module to bypass
		/// any security warning if WarningBypassMode = BypassWithQueryParam.
		/// </summary>
		[ConfigurationProperty("bypassQueryParamName")]
		public string BypassQueryParamName {
			get { return (string)this["bypassQueryParamName"]; }
			set { this["bypassQueryParamName"] = value; }
		}

		/// <summary>
		/// Gets or sets the path to a URI for encrypted redirections, if any.
		/// </summary>
		[ConfigurationProperty("encryptedUri"), RegexStringValidator(@"^(?:|(?:https://)?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")]
		public string EncryptedUri {
			get { return (string)this["encryptedUri"]; }
			set {
				if (value != null && value.Length > 0)
					this["encryptedUri"] = value;
				else
					this["encryptedUri"] = null;
			}
		}

		/// <summary>
		/// Gets or sets a flag indicating whether or not to maintain the current path when redirecting
		/// to a different host.
		/// </summary>
		[ConfigurationProperty("maintainPath", DefaultValue = true)]
		public bool MaintainPath {
			get { return (bool)this["maintainPath"]; }
			set { this["maintainPath"] = value; }
		}

		/// <summary>
		/// Gets or sets the mode indicating how the secure web page settings handled.
		/// </summary>
		[ConfigurationProperty("mode", DefaultValue = SecureWebPageMode.On)]
		public SecureWebPageMode Mode {
			get { return (SecureWebPageMode)this["mode"]; }
			set { this["mode"] = value; }
		}

		/// <summary>
		/// Gets the collection of directory settings read from the configuration section.
		/// </summary>
		[ConfigurationProperty("directories")]
		public SecureWebPageDirectorySettingCollection Directories {
			get { return (SecureWebPageDirectorySettingCollection)this["directories"]; }
		}

		/// <summary>
		/// Gets the collection of file settings read from the configuration section.
		/// </summary>
		[ConfigurationProperty("files")]
		public SecureWebPageFileSettingCollection Files {
			get { return (SecureWebPageFileSettingCollection)this["files"]; }
		}

		/// <summary>
		/// Gets or sets the path to a URI for unencrypted redirections, if any.
		/// </summary>
		[ConfigurationProperty("unencryptedUri"), RegexStringValidator(@"^(?:|(?:http://)?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")]
		public string UnencryptedUri {
			get { return (string)this["unencryptedUri"]; }
			set {
				if (value != null && value.Length > 0)
					this["unencryptedUri"] = value;
				else
					this["unencryptedUri"] = null;
			}
		}

		/// <summary>
		/// Gets or sets the bypass mode indicating whether or not to bypass security warnings
		/// when switching to a unencrypted page.
		/// </summary>
		[ConfigurationProperty("warningBypassMode", DefaultValue = SecurityWarningBypassMode.BypassWithQueryParam)]
		public SecurityWarningBypassMode WarningBypassMode {
			get { return (SecurityWarningBypassMode)this["warningBypassMode"]; }
			set { this["warningBypassMode"] = value; }
		}

		#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 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