Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

https on default ASP.NET MVC

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

like image 894
Eugene Sukhomlin Avatar asked Apr 25 '17 07:04

Eugene Sukhomlin


People also ask

What is the default extension for MVC URL?

MVC works with Extensionless URLs only (by default)<system.

What is the use of UseHttpsRedirection?

HTTPS Redirection Middleware (UseHttpsRedirection) to redirect HTTP requests to HTTPS. HSTS Middleware (UseHsts) to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.


2 Answers

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.

like image 83
Fei Han Avatar answered Oct 22 '22 01:10

Fei Han


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);
           }
}
like image 30
Houssam Hamdan Avatar answered Oct 22 '22 03:10

Houssam Hamdan