Click here to Skip to main content
15,897,371 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
Imports 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

	End Enum

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

#Region "Constructors"

		''' <summary>
		''' Creates an instance of SecureWebPageItemSetting.
		''' </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)
			Me.New()
			Me.Path = 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)
			Me.New(path)
			Me.Secure = secure
		End Sub

#End Region

#Region "Properties"

		''' <summary>
		''' Gets or sets the path of this item.
		''' </summary>
		<ConfigurationProperty("path", IsRequired:=True, IsKey:=True)> _
		Public Overridable Property Path() As String
			Get
				Return CStr(Me("path"))
			End Get
			Set(ByVal value As String)
				Me("path") = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the type of security for this item.
		''' </summary>
		<ConfigurationProperty("secure", DefaultValue:=SecurityType.Secure)> _
		Public Property Secure() As SecurityType
			Get
				Return CType(Me("secure"), SecurityType)
			End Get
			Set(ByVal value As SecurityType)
				Me("secure") = value
			End Set
		End Property

#End Region

	End Class

	''' <summary>
	''' Represents a collection of SecureWebPageItemSetting objects.
	''' </summary>
	Public MustInherit Class SecureWebPageItemSettingCollection
		Inherits 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 Function IndexOf(ByVal item As SecureWebPageItemSetting) As Integer
			If Not item Is Nothing Then
				Return BaseIndexOf(item)
			Else
				Return -1
			End If
		End Function

		''' <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 Function IndexOf(ByVal path As String) As Integer
			If path Is Nothing Then
				Throw New ArgumentNullException("path")
			Else
				Return Me.IndexOf(CType(BaseGet(path.ToLower(CultureInfo.InvariantCulture)), SecureWebPageItemSetting))
			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