aspnet / BasicMiddleware

[Archived] Basic middleware components for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
169 stars 84 forks source link

Request.IsHttps doesn't seem to like Azure + x-forwarded-* #41

Closed nulltoken closed 8 years ago

nulltoken commented 8 years ago

I'm running the following code on the official RC2 in an Azure WebApp.

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;

namespace WebApplication2
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.All
            });

            app.UseHttpMethodOverride();

            app.Use(async (context, next) =>
            {
                foreach (var header in context.Request.Headers)
                {
                    if (header.Key.StartsWith("x-f", StringComparison.OrdinalIgnoreCase))
                    await context.Response.WriteAsync($"{header.Key}: {header.Value}\r\n");
                }

                await context.Response.WriteAsync($"Method: {context.Request.Method}\r\n");
                await context.Response.WriteAsync($"Scheme: {context.Request.Scheme}\r\n");
                await context.Response.WriteAsync($"IsHttps: {context.Request.IsHttps}\r\n");

                await context.Response.WriteAsync("Hello World - " + DateTimeOffset.Now + Environment.NewLine);
                await context.Response.WriteAsync(Environment.NewLine);

                await context.Response.WriteAsync("Address:" + Environment.NewLine);
                await context.Response.WriteAsync("Host: " + context.Request.Headers["Host"] + Environment.NewLine);
                await context.Response.WriteAsync("PathBase: " + context.Request.PathBase.Value + Environment.NewLine);
                await context.Response.WriteAsync("Query: " + context.Request.QueryString.Value + Environment.NewLine);
                await context.Response.WriteAsync(Environment.NewLine);

                await context.Response.WriteAsync("Connection:" + Environment.NewLine);
                await context.Response.WriteAsync("RemoteIp: " + context.Connection.RemoteIpAddress + Environment.NewLine);
                await context.Response.WriteAsync("RemotePort: " + context.Connection.RemotePort + Environment.NewLine);
                await context.Response.WriteAsync("LocalIp: " + context.Connection.LocalIpAddress + Environment.NewLine);
                await context.Response.WriteAsync("LocalPort: " + context.Connection.LocalPort + Environment.NewLine);
                await context.Response.WriteAsync("ClientCert: " + context.Connection.ClientCertificate + Environment.NewLine);
                await context.Response.WriteAsync(Environment.NewLine);
            });
        }
    }
}

When issueing a request over http, the following is displayed

X-Forwarded-For: 78.192.yyy.xx:58475, 78.192.yyy.xx:58475
X-Forwarded-Proto: http
Method: GET
Scheme: http
IsHttps: False
Hello World - 5/29/2016 7:06:38 PM +00:00

Address:
Host: zzzzzzz.azurewebsites.net
PathBase: 
Query: ?

Connection:
RemoteIp: 127.0.0.1
RemotePort: 50068
LocalIp: 127.0.0.1
LocalPort: 11850
ClientCert: 

When issueing a request over https, the following is displayed

X-Forwarded-For: 78.192.yyy.xx:58531, 78.192.yyy.xx:58531
X-Forwarded-Proto: https
Method: GET
Scheme: http
**IsHttps: False**
RemoteIP: 127.0.0.1
RemotePort: 50132
Hello World - 5/29/2016 7:10:27 PM +00:00

Address:
Host: zzzzzzz.azurewebsites.net
PathBase: 
Query: ?

Connection:
RemoteIp: 127.0.0.1
RemotePort: 50132
LocalIp: 127.0.0.1
LocalPort: 11850
ClientCert: 

I must certainly be missing something obvious, but I can't see it. Any help would be greatly appreciated.

Tratcher commented 8 years ago

Yes, this is a known issue that has been addressed post RC2. https://github.com/aspnet/IISIntegration/issues/140