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
Imports System.Collections

Namespace Ventaur.Web.Security.Configuration

	''' <summary>
	''' Indicates the type of security for a SecureWebPageItem.
	''' </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>
	''' The SecureWebPageItemComparer class implements the IComparer interface to compare.
	''' </summary>
	Public Class SecureWebPageItemComparer
		Implements IComparer

		''' <summary>
		''' Compares the two objects as string and SecureWebPageItem or both SecureWebPageItem 
		''' by the Path property.
		''' </summary>
		''' <param name="x">First object to compare.</param>
		''' <param name="y">Second object to compare.</param>
		''' <returns></returns>
		Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
			' Check the type of the parameters
			If Not TypeOf x Is SecureWebPageItem AndAlso Not TypeOf x Is String Then
				' Throw an exception for the first argument
				Throw New ArgumentException("Parameter must be a SecureWebPageItem or a String.", "x")
			ElseIf Not TypeOf y Is SecureWebPageItem AndAlso Not TypeOf y Is String Then
				' Throw an exception for the second argument
				Throw New ArgumentException("Parameter must be a SecureWebPageItem or a String.", "y")
			End If

			' Initialize the path variables
			Dim xPath As String = String.Empty
			Dim yPath As String = String.Empty

			' Get the path for x
			If TypeOf x Is SecureWebPageItem Then
				xPath = CType(x, SecureWebPageItem).Path
			Else
				xPath = CType(x, String)
			End If

			' Get the path for y
			If TypeOf y Is SecureWebPageItem Then
				yPath = CType(y, SecureWebPageItem).Path
			Else
				yPath = CType(y, String)
			End If

			' Compare the paths, ignoring case
			Return String.Compare(xPath, yPath, True)
		End Function

	End Class


	''' <summary>
	''' The SecureWebPageItem class is the base class that represents entries in the &lt;secureWebPages&gt;
	''' configuration section.
	''' </summary>
	Public Class SecureWebPageItem
		' Fields
		Private _secure As SecurityType = SecurityType.Secure
		Private _path As String = String.Empty

		''' <summary>
		''' Gets or sets the type of security for this directory or file.
		''' </summary>
		Public Property Secure() As SecurityType
			Get
				Return _secure
			End Get
			Set(ByVal Value As SecurityType)
				_secure = Value
			End Set
		End Property

		''' <summary>
		''' Gets or sets the path of this directory or file.
		''' </summary>
		Public Property Path() As String
			Get
				Return _path
			End Get
			Set(ByVal Value As String)
				_path = Value
			End Set
		End Property

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

		''' <summary>
		''' Creates an instance with initial values.
		''' </summary>
		''' <param name="path">The relative path to the directory or file.</param>
		''' <param name="ignore">A flag to ignore security for the directory or file.</param>
		Public Sub New(ByVal path As String, ByVal secure As SecurityType)
			' Initialize the path and secure properties
			Me._path = path
			Me._secure = secure
		End Sub

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

	End Class

	''' <summary>
	''' The SecureWebPageItemCollection class houses a collection of SecureWebPageItem instances.
	''' </summary>
	Public Class SecureWebPageItemCollection
		Inherits CollectionBase

		''' <summary>
		''' Initialize an instance of this collection.
		''' </summary>
		Public Sub New()
			MyBase.New()
		End Sub

		''' <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 SecureWebPageItem) As Integer
			Return List.IndexOf(item)
		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
			' Create a comparer for sorting and searching
			Dim Comparer As New SecureWebPageItemComparer
			InnerList.Sort(Comparer)
			Return InnerList.BinarySearch(path, Comparer)
		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