Sunday, December 15, 2013

Redirecting HTTP to HTTPS in mvc

Make sure you have ssl certificate installed and both http and https bindings are set for the website.


In the Global.asax;

  protected void Application_BeginRequest()
        {            
            if (!Context.Request.IsSecureConnection)
                Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:")); 
        }

If you want to make it conditional to the production only, then using the DEBUG switch is an option;

 protected void Application_BeginRequest()
        {         
            #if !DEBUG
            if (!Context.Request.IsSecureConnection)
                Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
            #endif
        }

No comments:

Post a Comment