Don't have enough rep to add this as a comment, but I want to add that the WebHost.UseUrls()
in .net core 6
can be set using a combination of IPAddress
and IPEndPoint
in file Program.cs
if (!builder.Environment.IsDevelopment()) // app in release
{
// listen to any ip on port 80 for http
IPEndPoint ipEndPointHttp = new IPEndPoint(IPAddress.Any, 80),
// listen to any ip on port 443 for https
ipEndPointHttps = new IPEndPoint(IPAddress.Any, 443);
builder.WebHost.UseUrls($"http://{ipEndPointHttp}",
$"https://{ipEndPointHttps}");
// enforce ssl when in release
builder.Services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(3); // a commonly used value is one year.
});
// redirect to specific https port
builder.Services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
options.HttpsPort = ipEndPointHttps.Port;
});
}
else // app in debug
{
// listen to localhost on port 8081 for http
IPEndPoint ipEndPointHttp = new IPEndPoint(IPAddress.Loopback, 8081),
// listen to localhost on port 5001 for https
ipEndPointHttps = new IPEndPoint(IPAddress.Loopback, 5001);
builder.WebHost.UseUrls($"http://{ipEndPointHttp}",
$"https://{ipEndPointHttps}");
// redirect to specific https port
builder.Services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = (int)HttpStatusCode.TemporaryRedirect;
options.HttpsPort = ipEndPointHttps.Port;
});
}
... // rest of configuration
app.UseHttpsRedirection(); // set redirection to https if url is in http
....
Also note that using these values new IPEndPoint(IPAddress.Any, 80)
, *:80
, ::80
are all equivalent, but I prefer IPEndPoint since it is more verbose
If a custom ip address (ex: 192.168.1.200
) is needed other than IPAddress.Any
or IPAddress.Loopback
, a new address can be set using IPAddress.Parse("192.168.1.200")