0

I have an application hosted on IIS with an SSL certificate. with standard ASP I could configure rewrite in web.config for automatically redirecting to the SSL version.

But how can I do it in core? I want that when someone opens a link over HTTP they will be automatically redirected to HTTPS.

Many thank in advance!

Mahdi
  • 3,199
  • 2
  • 25
  • 35
  • Possible duplicate of [Redirect to HTTPS](http://stackoverflow.com/questions/29477393/redirect-to-https) – Set Mar 28 '17 at 06:09
  • No. This question is about app behind IIS, which terminates SSL session, and Kestrel will receive "clean http" traffic and may cause an infinite redirect loop. – Dmitry Mar 28 '17 at 08:44

2 Answers2

2

You still may (and should) use web.config

As soon as IIS deals with SSL - your app (Kestrel) receives "clean" HTTP, so it's too late to check for SSL connection in your code. You need to configure IIS to redirect from http to https.

I use this web.config (works in Azure):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
    <rewrite>
      <rules>
        <clear />
        <rule name="Redirect to https" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Dmitry
  • 16,110
  • 4
  • 61
  • 73
0

In Asp.Net 2.1 and above, in your startup.cs simply add the following in the app Configure method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // other app configure code here

    app.UseHttpsRedirection();

}
Michael Armitage
  • 1,502
  • 19
  • 17