Use ASPNETCORE_HTTPS_PORT to get port from config (#266)
This commit is contained in:
parent
8119c974ad
commit
ff5aed132b
|
|
@ -21,7 +21,7 @@ namespace HttpsSample
|
|||
services.AddHttpsRedirection(options =>
|
||||
{
|
||||
options.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
|
||||
options.TlsPort = 5001;
|
||||
options.HttpsPort = 5001;
|
||||
});
|
||||
|
||||
services.AddHsts(options =>
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Rewrite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
|
|
@ -31,10 +32,25 @@ namespace Microsoft.AspNetCore.Builder
|
|||
|
||||
var options = app.ApplicationServices.GetRequiredService<IOptions<HttpsRedirectionOptions>>().Value;
|
||||
|
||||
// The tls port set in options will have priority over the one in configuration.
|
||||
var httpsPort = options.HttpsPort;
|
||||
if (httpsPort == null)
|
||||
{
|
||||
// Only read configuration if there is no httpsPort
|
||||
var config = app.ApplicationServices.GetRequiredService<IConfiguration>();
|
||||
var configHttpsPort = config["HTTPS_PORT"];
|
||||
// If the string isn't empty, try to parse it.
|
||||
if (!string.IsNullOrEmpty(configHttpsPort)
|
||||
&& int.TryParse(configHttpsPort, out var intHttpsPort))
|
||||
{
|
||||
httpsPort = intHttpsPort;
|
||||
}
|
||||
}
|
||||
|
||||
var rewriteOptions = new RewriteOptions();
|
||||
rewriteOptions.AddRedirectToHttps(
|
||||
options.RedirectStatusCode,
|
||||
options.TlsPort);
|
||||
httpsPort);
|
||||
|
||||
app.UseRewriter(rewriteOptions);
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,6 @@ namespace Microsoft.AspNetCore.HttpsPolicy
|
|||
/// <remarks>
|
||||
/// Defaults to 443 if not provided.
|
||||
/// </remarks>
|
||||
public int? TlsPort { get; set; }
|
||||
public int? HttpsPort { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ namespace Microsoft.AspNetCore.Builder
|
|||
{
|
||||
throw new ArgumentNullException(nameof(configureOptions));
|
||||
}
|
||||
|
||||
services.Configure(configureOptions);
|
||||
return services;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
services.Configure<HttpsRedirectionOptions>(options =>
|
||||
{
|
||||
options.RedirectStatusCode = statusCode;
|
||||
options.TlsPort = tlsPort;
|
||||
options.HttpsPort = tlsPort;
|
||||
});
|
||||
services.Configure<HstsOptions>(options =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
[InlineData(308, null, "https://localhost/")]
|
||||
[InlineData(301, 5050, "https://localhost:5050/")]
|
||||
[InlineData(301, 443, "https://localhost/")]
|
||||
public async Task SetOptions_SetStatusCodeTlsPort(int statusCode, int? tlsPort, string expected)
|
||||
public async Task SetOptions_SetStatusCodeHttpsPort(int statusCode, int? httpsPort, string expected)
|
||||
{
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
|
|
@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
services.Configure<HttpsRedirectionOptions>(options =>
|
||||
{
|
||||
options.RedirectStatusCode = statusCode;
|
||||
options.TlsPort = tlsPort;
|
||||
options.HttpsPort = httpsPort;
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
|
|
@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
[InlineData(308, null, "https://localhost/")]
|
||||
[InlineData(301, 5050, "https://localhost:5050/")]
|
||||
[InlineData(301, 443, "https://localhost/")]
|
||||
public async Task SetOptionsThroughHelperMethod_SetStatusCodeTlsPort(int statusCode, int? tlsPort, string expectedUrl)
|
||||
public async Task SetOptionsThroughHelperMethod_SetStatusCodeAndHttpsPort(int statusCode, int? httpsPort, string expectedUrl)
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(services =>
|
||||
|
|
@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
services.AddHttpsRedirection(options =>
|
||||
{
|
||||
options.RedirectStatusCode = statusCode;
|
||||
options.TlsPort = tlsPort;
|
||||
options.HttpsPort = httpsPort;
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
|
|
@ -121,5 +121,41 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests
|
|||
Assert.Equal(statusCode, (int)response.StatusCode);
|
||||
Assert.Equal(expectedUrl, response.Headers.Location.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null, "https://localhost/")]
|
||||
[InlineData(null, "5000", "https://localhost:5000/")]
|
||||
[InlineData(null, "443", "https://localhost/")]
|
||||
[InlineData(443, "5000", "https://localhost/")]
|
||||
[InlineData(4000, "5000", "https://localhost:4000/")]
|
||||
[InlineData(5000, null, "https://localhost:5000/")]
|
||||
public async Task SetHttpsPortEnvironmentVariable_ReturnsCorrectStatusCodeOnResponse(int? optionsHttpsPort, string configHttpsPort, string expectedUrl)
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddHttpsRedirection(options =>
|
||||
{
|
||||
options.HttpsPort = optionsHttpsPort;
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseHttpsRedirection();
|
||||
app.Run(context =>
|
||||
{
|
||||
return context.Response.WriteAsync("Hello world");
|
||||
});
|
||||
});
|
||||
builder.UseSetting("HTTPS_PORT", configHttpsPort);
|
||||
var server = new TestServer(builder);
|
||||
var client = server.CreateClient();
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "");
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
|
||||
Assert.Equal(expectedUrl, response.Headers.Location.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue