title: ASP.NET Core 2.1 no HTTP/HTTPS redirection in App Engine
body: <h1>Problem</h1>
  
  <p>I could not get the automatic redirection from HTTP to HTTPS to work correctly when the app is published to App Engine.</p>
  
  <p>When i access the website through <em>example.com</em> the site got routed to <a href="http://www.example.com" rel="noreferrer">http://www.example.com</a> and show that the connection is unsecured.
  When i access the website through <a href="https://www.example.com" rel="noreferrer">https://www.example.com</a> the website is then correctly secured with google-managed SSL. However the automatic redirection from HTTP to HTTPS does not occurs.</p>
  
  <p><a href="https://i.stack.imgur.com/Jaumj.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Jaumj.png" alt="Insecured connection"></a></p>
  
  <p>I also got an error in Log Viewer warning that Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware was throwing Failed to determine the https port for redirect.</p>
  
  <p><a href="https://i.stack.imgur.com/WN5Pi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/WN5Pi.png" alt="enter image description here"></a></p>
  
  <p>I have followed the documentation from MSDN and only get it to work locally, but not when the app is published to App Engine.
  <a href="https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.1&amp;tabs=visual-studio" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.1&amp;tabs=visual-studio</a></p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          app.UseDatabaseErrorPage();
      }
      else
      {
          app.UseStatusCodePages();
          app.UseExceptionHandler("/Error");
          app.UseHsts(); // This was added by the template
      }
  
      app.UseHttpsRedirection(); // This was added by the template
      app.UseStaticFiles();
      app.UseCookiePolicy();
      app.UseAuthentication();
      app.UseMvc();
  }
  </code></pre>
  
  <p>Here is the Program.cs. Basically default from the project template</p>
  
  <pre><code>public static IWebHostBuilder CreateWebHostBuilder(string[] args)
  {
      return WebHost.CreateDefaultBuilder(args)
          .CaptureStartupErrors(true)
          .UseStartup&lt;Startup&gt;();
  }
  </code></pre>
  
  <p>The app.yaml used for the deployment</p>
  
  <pre><code>runtime: aspnetcore
  env: flexible
  automatic_scaling:
    min_num_instances: 1
    max_num_instances: 20
    cpu_utilization:
      target_utilization: 0.8
  readiness_check:
    path: "/readinesscheck"
    check_interval_sec: 5
    timeout_sec: 4
    failure_threshold: 2
    success_threshold: 2
    app_start_timeout_sec: 300
  liveness_check:
    path: "/livenesscheck"
    check_interval_sec: 30
    timeout_sec: 4
    failure_threshold: 2
    success_threshold: 2
  skip_files:
    - node_modules/
    - wwwroot/src/vendor/
    - ^(.*/)?.*\.pdb$
    - ^(.*/)?.*\.log$
  </code></pre>
  
  <h1>What I've tried was the following (Only one at a time)</h1>
  
  <ol>
  <li>AddHttpsRedirection Middleware to the ConfigureServices method. </li>
  </ol>
  
  <p>Which ended up with app being inaccessible (502 Server Error).</p>
  
  <pre><code>services.AddHttpsRedirection(options =&gt;
  {
      options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
      options.HttpsPort = 443;
  });
  </code></pre>
  
  <ol start="2">
  <li>Add EnvironmentVariable to app.yaml</li>
  </ol>
  
  <p>Also ended up with app being inaccessible (502 Server Error).</p>
  
  <pre><code>env_variables:
     ASPNETCORE_HTTPS_PORT: "443"
  </code></pre>
  
  <ol start="3">
  <li>Manually configuring the HTTPS port in Program.cs</li>
  </ol>
  
  <p>Also ended up with app being inaccessible (502 Server Error).</p>
  
  <pre><code>WebHost.CreateDefaultBuilder(args)
      .UseSetting("https_port", "8080") // also return 502 when port is 443
  </code></pre>
  
  <ol start="4">
  <li>Configure ForwardedHeaderOptions in ConfigureServices method and Use ForwardedHeaderOptions in Configure method. <a href="https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1#other-proxy-server-and-load-balancer-scenarios" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1#other-proxy-server-and-load-balancer-scenarios</a></li>
  </ol>
  
  <p>App is accessible but no automatic HTTP/HTTPS redirection. </p>
  
  <pre><code>services.Configure&lt;ForwardedHeadersOptions&gt;(options =&gt;
  {
      options.ForwardedHeaders = 
          ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
  });
  
  app.UseForwardedHeaders();
  </code></pre>
  
  <ol start="5">
  <li>Manually exposing port 443 and 8080 in Dockerfile.</li>
  </ol>
  
  <p>App is accessible but no automatic HTTP/HTTPS redirection. I understand that when runtime in <strong>app.yaml</strong> is set to <strong>aspnetcore</strong>. The publish process automatically generated it's own Dockerfile which is used to deploy the app to App Engine.</p>
  
  <pre><code>EXPOSE 443
  EXPOSE 8080
  </code></pre>