title: .net Core X Forwarded Proto not working
body: <p>I am working to get my .net core 1.1 application working behind a load balancer and enforcing https. I have the following setup in my Startup.cs</p>
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, IOptions&lt;Auth0Settings&gt; auth0Settings)
  {
      loggerFactory.AddConsole(Configuration.GetSection(&quot;Logging&quot;));
      loggerFactory.AddDebug();
  
  
      var startupLogger = loggerFactory.CreateLogger&lt;Startup&gt;();
  
  
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
          app.UseDatabaseErrorPage();
          app.UseBrowserLink();
          startupLogger.LogInformation(&quot;In Development&quot;);
      }
      else
      {
          startupLogger.LogInformation(&quot;NOT in development&quot;);
          app.UseExceptionHandler(&quot;/Home/Error&quot;);
      }
  
      app.UseMiddleware&lt;HttpsRedirectMiddleware&gt;();
      app.UseForwardedHeaders(new ForwardedHeadersOptions
      {
          ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
      });`
          app.UseCookieAuthentication(new CookieAuthenticationOptions
          {
              AuthenticationScheme= CookieAuthenticationDefaults.AuthenticationScheme,
              AutomaticAuthenticate = true,
              AutomaticChallenge = true,
              CookieHttpOnly = true,
              SlidingExpiration = true
          });
  </code></pre>
  <p>The HttpsRedirectMiddleware is for validating the LB has the X-Forwarded-Proto set, it does, and comes back as https as the only value. When I go to the site (<a href="https://myapp.somedomain.net" rel="noreferrer">https://myapp.somedomain.net</a>), it knows I am not authenticated and redirects me to (<a href="http://myapp.somedomain.net/Account/Logon?ReturnUrl=%2f" rel="noreferrer">http://myapp.somedomain.net/Account/Logon?ReturnUrl=%2f</a>). It loses the SSL connection and switched back over to port 80 on me. The .net core documentation says to use &quot;UseForwardedHeaders&quot; like below, which does not work in my case. The console logger does not have any error or warnings from the middleware when this switch happens.</p>
  <p>For a short term fix, I have put this below &quot;UseForwardedHeaders&quot;</p>
  <pre><code>    app.Use(async (context, next) =&gt;
      {
          var xproto = context.Request.Headers[&quot;X-Forwarded-Proto&quot;].ToString();
          if (xproto!=null &amp;&amp; xproto.StartsWith(&quot;https&quot;, StringComparison.OrdinalIgnoreCase)){
              startupLogger.LogInformation(&quot;Switched to https&quot;);
              context.Request.Scheme = &quot;https&quot;;
          }
          await next();
  
      });
  </code></pre>
  <p>The above works perfect, but is a hack. I would like to do it the correct way.</p>