Click here to Skip to main content
15,893,401 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.6M   680  
An article on automatically switching between HTTP and HTTPS protocols without hard-coding absolute URLs
Imports System
Imports System.Configuration
Imports System.Globalization

Namespace Ventaur.Web.Security.Configuration

	''' <summary>
	''' Represents an file entry in the &lt;secureWebPages&gt;
	''' configuration section.
	''' </summary>
	Public Class SecureWebPageFileSetting
		Inherits SecureWebPageItemSetting

#Region "Constructors"

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

		''' <summary>
		''' Creates an instance with an initial path value.
		''' </summary>
		''' <param name="path">The relative path to the file.</param>
		Public Sub New(ByVal path As String)
			MyBase.New(path)
		End Sub

		''' <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 Sub New(ByVal path As String, ByVal secure As SecurityType)
			MyBase.New(path, secure)
		End Sub

#End Region

		''' <summary>
		''' Gets or sets the path of this file setting.
		''' </summary>
		<ConfigurationProperty("path", IsRequired:=True, IsKey:=True), RegexStringValidator("^(?:|[\w\-][\w\.\-,]*(?:/[\w\.\-]+)*)$")> _
		Public Overrides Property Path() As String
			Get
				Return MyBase.Path
			End Get
			Set(ByVal value As String)
				MyBase.Path = value
			End Set
		End Property

	End Class

	''' <summary>
	''' Represents a collection of SecureWebPageFileSetting objects.
	''' </summary>
	Public Class SecureWebPageFileSettingCollection
		Inherits SecureWebPageItemSettingCollection

		''' <summary>
		''' Gets the element name for this collection.
		''' </summary>
		Protected Overrides ReadOnly Property ElementName() As String
			Get
				Return "files"
			End Get
		End Property

		''' <summary>
		''' Gets a flag indicating an exception should be thrown if a duplicate element 
		''' is added to the collection.
		''' </summary>
		Protected Overrides ReadOnly Property ThrowOnDuplicate() As Boolean
			Get
				Return True
			End Get
		End Property

		''' <summary>
		''' Gets the element at the specified index.
		''' </summary>
		''' <param name="index">The index to retrieve the element from.</param>
		''' <returns>The SecureWebPageFileSetting located at the specified index.</returns>
		Default Public Shadows ReadOnly Property Item(ByVal index As Integer) As SecureWebPageFileSetting
			Get
				Return CType(BaseGet(index), SecureWebPageFileSetting)
			End Get
		End Property

		''' <summary>
		''' Gets the element with the specified path.
		''' </summary>
		''' <param name="path">The path of the element to retrieve.</param>
		''' <returns>The SecureWebPageFileSetting with the specified path.</returns>
		Default Public Shadows ReadOnly Property Item(ByVal path As String) As SecureWebPageFileSetting
			Get
				If path Is Nothing Then
					Throw New ArgumentNullException("path")
				Else
					Return CType(BaseGet(path.ToLower(CultureInfo.InvariantCulture)), SecureWebPageFileSetting)
				End If
			End Get
		End Property

#Region "Collection Methods"

		''' <summary>
		''' Adds a SecureWebPageFileSetting to the collection.
		''' </summary>
		''' <param name="fileSetting">An initialized SecureWebPageFileSetting instance.</param>
		Public Sub Add(ByVal fileSetting As SecureWebPageFileSetting)
			BaseAdd(fileSetting)
		End Sub

		''' <summary>
		''' Clears all file entries from the collection.
		''' </summary>
		Public Sub Clear()
			BaseClear()
		End Sub

		''' <summary>
		''' Removes the specified SecureWebPageFileSetting from the collection, if it exists.
		''' </summary>
		''' <param name="fileSetting">A SecureWebPageFileSetting to remove.</param>
		Public Sub Remove(ByVal fileSetting As SecureWebPageFileSetting)
			Dim Index As Integer = MyBase.IndexOf(fileSetting)
			If (Index >= 0) Then
				BaseRemoveAt(Index)
			End If
		End Sub

		''' <summary>
		''' Removes a SecureWebPageFileSetting from the collection with a matching path as specified.
		''' </summary>
		''' <param name="path">The path of a SecureWebPageFileSetting to remove.</param>
		Public Sub Remove(ByVal path As String)
			Dim Index As Integer = MyBase.IndexOf(path)
			If (Index >= 0) Then
				BaseRemoveAt(Index)
			End If
		End Sub

		''' <summary>
		''' Removes the SecureWebPageFileSetting from the collection at the specified index.
		''' </summary>
		''' <param name="index">The index of the SecureWebPageFileSetting to remove.</param>
		Public Sub RemoveAt(ByVal index As Integer)
			BaseRemoveAt(index)
		End Sub

#End Region

		''' <summary>
		''' Creates a new element for this collection.
		''' </summary>
		''' <returns>A new instance of SecureWebPageFileSetting.</returns>
		Protected Overrides Function CreateNewElement() As ConfigurationElement
			Return New SecureWebPageFileSetting()
		End Function

		''' <summary>
		''' Gets the key for the specified element.
		''' </summary>
		''' <param name="element">An element to get a key for.</param>
		''' <returns>A string containing the Path of the SecureWebPageFileSetting.</returns>
		Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
			If Not element Is Nothing Then
				Return CType(element, SecureWebPageFileSetting).Path.ToLower(CultureInfo.InvariantCulture)
			Else
				Return Nothing
			End If
		End Function

	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