diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/IISSetupFilter.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/IISSetupFilter.cs index 1a9f351205..6c2aa54b95 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/IISSetupFilter.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/IISSetupFilter.cs @@ -4,22 +4,26 @@ using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.Server.IISIntegration { internal class IISSetupFilter : IStartupFilter { - private string _pairingToken; + private readonly string _pairingToken; + private readonly PathString _pathBase; - internal IISSetupFilter(string pairingToken) + internal IISSetupFilter(string pairingToken, PathString pathBase) { _pairingToken = pairingToken; + _pathBase = pathBase; } public Action Configure(Action next) { return app => { + app.UsePathBase(_pathBase); app.UseForwardedHeaders(); app.UseMiddleware(_pairingToken); next(app); diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs index 39ba3aefdf..d6746b1f16 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Server.IISIntegration; using Microsoft.Extensions.DependencyInjection; @@ -35,14 +36,14 @@ namespace Microsoft.AspNetCore.Hosting if (!string.IsNullOrEmpty(port) && !string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(pairingToken)) { - var address = "http://localhost:" + port + path; + var address = "http://localhost:" + port; hostBuilder.CaptureStartupErrors(true); hostBuilder.ConfigureServices(services => { // Delay register the url so users don't accidently overwrite it. hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, address); - services.AddSingleton(new IISSetupFilter(pairingToken)); + services.AddSingleton(new IISSetupFilter(pairingToken, new PathString(path))); services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; diff --git a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs index f2e49fb97c..ea1be889e5 100644 --- a/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.Server.IISIntegration.Tests/IISMiddlewareTests.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -79,14 +78,64 @@ namespace Microsoft.AspNetCore.Server.IISIntegration .UseSetting("TOKEN", "TestToken") .UseSetting("PORT", "12345") .UseSetting("APPL_PATH", "/") - .UseIISIntegration(); + .UseIISIntegration() + .Configure(app => + { + app.Run(context => Task.FromResult(0)); + }); Assert.Null(builder.GetSetting(WebHostDefaults.ServerUrlsKey)); // Adds a server and calls Build() var server = new TestServer(builder); - Assert.Equal("http://localhost:12345/", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); + Assert.Equal("http://localhost:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); + } + + [Fact] + public void PathBaseHiddenFromServer() + { + var builder = new WebHostBuilder() + .UseSetting("TOKEN", "TestToken") + .UseSetting("PORT", "12345") + .UseSetting("APPL_PATH", "/pathBase") + .UseIISIntegration() + .Configure(app => + { + app.Run(context => Task.FromResult(0)); + }); + new TestServer(builder); + + Assert.Equal("http://localhost:12345", builder.GetSetting(WebHostDefaults.ServerUrlsKey)); + } + + [Fact] + public async Task AddsUsePathBaseMiddlewareWhenPathBaseSpecified() + { + var requestPathBase = string.Empty; + var requestPath = string.Empty; + var builder = new WebHostBuilder() + .UseSetting("TOKEN", "TestToken") + .UseSetting("PORT", "12345") + .UseSetting("APPL_PATH", "/pathbase") + .UseIISIntegration() + .Configure(app => + { + app.Run(context => + { + requestPathBase = context.Request.PathBase.Value; + requestPath = context.Request.Path.Value; + return Task.FromResult(0); + }); + }); + var server = new TestServer(builder); + + var request = new HttpRequestMessage(HttpMethod.Get, "/PathBase/Path"); + request.Headers.TryAddWithoutValidation("MS-ASPNETCORE-TOKEN", "TestToken"); + var response = await server.CreateClient().SendAsync(request); + + Assert.Equal("/PathBase", requestPathBase); + Assert.Equal("/Path", requestPath); } [Fact] @@ -119,7 +168,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration Assert.True(assertsExecuted); } - [Fact] public async Task DoesNotAddAuthenticationHandlerIfWindowsAuthDisabled() {