How to force SSL on your ASP.NET website?

I have seen may implementations of forcing SSL on websites that require changes to IIS and creating dummy aspx pages for the redirect, but nothing beats the simple and elegant code that follows:

Courtesy: IKOSoftware

// Force SSL
//
Uri currentUrl = System.Web.HttpContext.Request.Url;
if (!currentUrl.IsLoopback)
{
    if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, 
                                  StringComparison.CurrentCultureIgnoreCase))
    {
        // Build the Secure Uri
        //
        UriBuilder secureUrlBuilder = new UriBuilder(currentUrl);
        secureUrlBuilder.Scheme = Uri.UriSchemeHttps;
    
        // Use the default port.
        //
        secureUrlBuilder.Port = -1;
    
        // Redirect and end the response
        //
        System.Web.HttpContext.Response.Redirect(secureUrlBuilder.Uri.ToString());
    }
}


3 Responses

RE: How to force SSL on your ASP.NET website?

88YbYM http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com

- Barnypok   (http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com)   July 08, 2017 09:45 AM

RE: How to force SSL on your ASP.NET website?

Who would I report to? http://avanafil.blog.hu/ waistcoat unable buy avanafil chip One of the reasons it's difficult to prepare for retirement is that there are so many things we simply can't predict about it. We don't know how long we will live, how much our investments will earn and how much of our purchasing power inflation will erode. Here are some of the scariest retirement unknowns and how to cope with them:

- Joesph   (http://avanafil.blog.hu/)   December 06, 2017 01:55 PM

RE: How to force SSL on your ASP.NET website?

t8yTOk https://goldentabs.com/

- GoldenTabs   (https://goldentabs.com/)   January 07, 2018 07:28 AM

Leave a Reply

Please enter all required information

*

*

*