6

I have ASP.NET MVC web app.

When I upload it on Azure web app, website has http://

I need it to be https:// always.

How can I do this?

I know that it may be the too broad question, but can you give me some advice?

enter image description here

I set properties like this in my project

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
Eugene Sukhomlin
  • 89
  • 1
  • 1
  • 9

2 Answers2

10

I need it to be https:// always.

You could try to create a URL Rewrite rule to enforce HTTPS.

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
   <system.webServer>
     <rewrite>
       <rules>
         <!-- BEGIN rule TAG FOR HTTPS REDIRECT -->
         <rule name="Force HTTPS" enabled="true">
           <match url="(.*)" ignoreCase="false" />
           <conditions>
             <add input="{HTTPS}" pattern="off" />
           </conditions>
           <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
         </rule>
         <!-- END rule TAG FOR HTTPS REDIRECT -->
       </rules>
     </rewrite>
   </system.webServer>
 </configuration>

You could also refer to Enforce HTTPS on your app.

Fei Han
  • 26,415
  • 1
  • 30
  • 41
  • And don't forget to check if the server has URL rewriting module installed as described on this [post](https://stackoverflow.com/questions/16836473/asp-net-http-error-500-19-internal-server-error-0x8007000d) – regisls Sep 21 '20 at 15:03
7

If you prefer a solution through code and not web.config,, you can write the following in Global.asax.cs file.

protected void Application_BeginRequest() {
           // Ensure any request is returned over SSL/TLS in production
           if (!Request.IsLocal && !Context.Request.IsSecureConnection) {
               var redirect = Context.Request.Url.ToString().ToLower(CultureInfo.CurrentCulture).Replace("http:", "https:");
               Response.Redirect(redirect);
           }
}
Houssam Hamdan
  • 888
  • 6
  • 15