Click here to Skip to main content
15,888,527 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
Imports System
Imports System.Configuration
Imports 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

	End Enum


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

	End Enum


	''' <summary>
	''' Contains the settings of a secureWebPages configuration section.
	''' </summary>
	Public Class SecureWebPageSettings
		Inherits ConfigurationSection

		''' <summary>
		''' Creates an instance of SecureWebPageSettings.
		''' </summary>
		Public Sub New()
			MyBase.New()
		End Sub

#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 Property BypassQueryParamName() As String
			Get
				Return CStr(Me("bypassQueryParamName"))
			End Get
			Set(ByVal value As String)
				Me("bypassQueryParamName") = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the path to a URI for encrypted redirections, if any.
		''' </summary>
		<ConfigurationProperty("encryptedUri"), RegexStringValidator("^(?:|(?:https:')?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")> _
		Public Property EncryptedUri() As String
			Get
				Return CStr(Me("encryptedUri"))
			End Get
			Set(ByVal value As String)
				If Not value Is Nothing AndAlso value.Length > 0 Then
					Me("encryptedUri") = value
				Else
					Me("encryptedUri") = Nothing
				End If
			End Set
		End Property

		''' <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 Property MaintainPath() As Boolean
			Get
				Return CBool(Me("maintainPath"))
			End Get
			Set(ByVal value As Boolean)
				Me("maintainPath") = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the mode indicating how the secure web page settings handled.
		''' </summary>
		<ConfigurationProperty("mode", DefaultValue:=SecureWebPageMode.On)> _
		Public Property Mode() As SecureWebPageMode
			Get
				Return CType(Me("mode"), SecureWebPageMode)
			End Get
			Set(ByVal value As SecureWebPageMode)
				Me("mode") = value
			End Set
		End Property

		''' <summary>
		''' Gets the collection of directory settings read from the configuration section.
		''' </summary>
		<ConfigurationProperty("directories")> _
		Public ReadOnly Property Directories() As SecureWebPageDirectorySettingCollection
			Get
				Return CType(Me("directories"), SecureWebPageDirectorySettingCollection)
			End Get
		End Property

		''' <summary>
		''' Gets the collection of file settings read from the configuration section.
		''' </summary>
		<ConfigurationProperty("files")> _
		Public ReadOnly Property Files() As SecureWebPageFileSettingCollection
			Get
				Return CType(Me("files"), SecureWebPageFileSettingCollection)
			End Get
		End Property

		''' <summary>
		''' Gets or sets the path to a URI for unencrypted redirections, if any.
		''' </summary>
		<ConfigurationProperty("unencryptedUri"), RegexStringValidator("^(?:|(?:http:')?[\w\-][\w\.\-]*(?:/[\w\.\-]+)*/?)$")> _
		Public Property UnencryptedUri() As String
			Get
				Return CStr(Me("unencryptedUri"))
			End Get
			Set(ByVal value As String)
				If Not value Is Nothing AndAlso value.Length > 0 Then
					Me("unencryptedUri") = value
				Else
					Me("unencryptedUri") = Nothing
				End If
			End Set
		End Property

		''' <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 Property WarningBypassMode() As SecurityWarningBypassMode
			Get
				Return CType(Me("warningBypassMode"), SecurityWarningBypassMode)
			End Get
			Set(ByVal value As SecurityWarningBypassMode)
				Me("warningBypassMode") = value
			End Set
		End Property

#End Region

	End Class

End Namespace

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