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?
I set properties like this in my project
MVC works with Extensionless URLs only (by default)<system.
HTTPS Redirection Middleware (UseHttpsRedirection) to redirect HTTP requests to HTTPS. HSTS Middleware (UseHsts) to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With