Force a page to use HTTPS instead of HTTP
The following sample code will force a page to use ssl woth asp.net . Please add code in to Page_Load event
C# Code
//this is the current url
System.Uri currentUrl = System.Web.HttpContext.Current.Request.Url;
//don't redirect if this is localhost
if (!currentUrl.IsLoopback)
{
if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase))
{
//build the secure uri
System.UriBuilder secureUrlBuilder = new UriBuilder(currentUrl);
secureUrlBuilder.Scheme = Uri.UriSchemeHttps;
//use the default port.
secureUrlBuilder.Port = -1;
//redirect and end the response.
System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString());
}
}
VB.NET Code
'this is the current url
Dim currentUrl As System.Uri = System.Web.HttpContext.Current.Request.Url
'don't redirect if this is localhost
If Not currentUrl.IsLoopback Then
If Not currentUrl.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.CurrentCultureIgnoreCase) Then
'build the secure uri
Dim secureUrlBuilder As System.UriBuilder = New UriBuilder(currentUrl)
secureUrlBuilder.Scheme = Uri.UriSchemeHttps
'use the default port.
secureUrlBuilder.Port = -1
'redirect and end the response.
System.Web.HttpContext.Current.Response.Redirect(secureUrlBuilder.Uri.ToString())
End If
End If
No comments :
Post a Comment