Click here to Skip to main content
15,886,005 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.Collections.Specialized

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>
	''' SecureWebPageSettings contains the settings of a secureWebPages configuration section.
	''' </summary>
	Public Class SecureWebPageSettings

		' Fields
		Private _bypassQueryParamName As String = "BypassSecurityWarning"
		Private _encryptedUri As String = String.Empty
		Private _maintainPath As Boolean = True
		Private _mode As SecureWebPageMode = SecureWebPageMode.On
		Private _directories As SecureWebPageDirectoryCollection
		Private _files As SecureWebPageFileCollection
		Private _unencryptedUri As String = String.Empty
		Private _warningBypassMode As SecurityWarningBypassMode = SecurityWarningBypassMode.BypassWithQueryParam

#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>
		Public Property BypassQueryParamName() As String
			Get
				Return _bypassQueryParamName
			End Get
			Set(ByVal Value As String)
				_bypassQueryParamName = Value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the path to a URI for encrypted redirections, if any.
		''' </summary>
		Public Property EncryptedUri() As String
			Get
				Return _encryptedUri
			End Get
			Set(ByVal Value As String)
				If Not Value Is Nothing AndAlso Value.Length > 0 Then
					_encryptedUri = Value
				Else
					_encryptedUri = String.Empty
				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>
		Public Property MaintainPath() As Boolean
			Get
				Return _maintainPath
			End Get
			Set(ByVal Value As Boolean)
				_maintainPath = Value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the mode indicating how the secure web page settings handled.
		''' </summary>
		Public Property Mode() As SecureWebPageMode
			Get
				Return _mode
			End Get
			Set(ByVal Value As SecureWebPageMode)
				_mode = Value
			End Set
		End Property

		''' <summary>
		''' Gets the collection of directories read from the configuration section.
		''' </summary>
		Public ReadOnly Property Directories() As SecureWebPageDirectoryCollection
			Get
				Return _directories
			End Get
		End Property

		''' <summary>
		''' Gets the collection of files read from the configuration section.
		''' </summary>
		Public ReadOnly Property Files() As SecureWebPageFileCollection
			Get
				Return _files
			End Get
		End Property

		''' <summary>
		''' Gets or sets the path to a URI for unencrypted redirections, if any.
		''' </summary>
		Public Property UnencryptedUri() As String
			Get
				Return _unencryptedUri
			End Get
			Set(ByVal Value As String)
				If Not Value Is Nothing AndAlso Value.Length > 0 Then
					_unencryptedUri = Value
				Else
					_unencryptedUri = String.Empty
				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>
		Public Property WarningBypassMode() As SecurityWarningBypassMode
			Get
				Return _warningBypassMode
			End Get
			Set(ByVal Value As SecurityWarningBypassMode)
				_warningBypassMode = Value
			End Set
		End Property

#End Region

		''' <summary>
		''' The default constructor creates the needed lists.
		''' </summary>
		Public Sub New()
			' Create the collections
			_directories = New SecureWebPageDirectoryCollection
			_files = New SecureWebPageFileCollection
		End Sub

	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