Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

How to redirect all http requests to https in C sharp and Umbraco

using System;
using System.Web;
namespace Web.Model.HttpsModule
{
    /// <summary>
    /// Redirects all pages to https.
    /// </summary>
    public class RedirectToHttps : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += (app_BeginRequest);
        }
        private void app_BeginRequest(object sender, EventArgs e)
        {
            int securePort = 443;
            if (!HttpContext.Current.Request.IsSecureConnection || HttpContext.Current.Request.Url.Port != securePort)
            {
                string redirectUrl = HttpContext.Current.Request.Url.ToString().Replace("http:", "https:");
                HttpContext.Current.Response.RedirectPermanent(redirectUrl);
            }
        }
        public void Dispose()
        {
            // Needed for IHttpModule
        }
    }
}

Please enter your comment.