I would like to setup HTTPS for my ASP.NET core application as well as defining different ports on which my app can listen to (e.g. a bit like what was shown by Scot Hanselman a while ago: https://channel9.msdn.com/Events/Build/2017/B8048).
I have checked the official resources:
- https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl
- https://docs.microsoft.com/en-us/aspnet/core/security/https
I thought that the default builder was supposed to dig in the appsettings.json (or the appsettings.Development.json when it is relevant).
Program.cs content:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var builder = WebHost.CreateDefaultBuilder(args);
return builder.UseStartup<Startup>().Build();
}
}
appsettings.Development.json content:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
},
"Kestrel": {
"Endpoints": {
"Localhost": {
"Address": "127.0.0.1",
"Port": "5001"
}
}
},
"LocalhostwithHttps": {
"Address": "127.0.0.1",
"Port": "44333",
"Certificate": {
"Source": "Store",
"StoreLocation": "LocalMachine",
"StoreName": "My",
"Subject": "CN=localhost"
}
}
}
Alas, when starting debugging against the development environment, my web app is ending up starting like this:
Hosting environment: Development
Content root path: c:\Users\ehoua\Desktop\There
Launching browser (cmd.exe /C start http://localhost:5000)
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Do I have to really give up on the default builder and specify either the appsettings on which the the app needs to rely on or to hardcode the port values...?
[EDIT]
Following @Set answer, I tried the following in Program.cs
:
public static IWebHost BuildWebHost(string[] args)
{
var developmentConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.Development.json", optional: false)
.Build();
var productionConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json", optional: false)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseEnvironment("Development").UseConfiguration(developmentConfiguration)
.UseEnvironment("Production").UseConfiguration(productionConfiguration)
.UseStartup<Startup>().Build();
}
I purposely set the optional argument to false in order to double check whether the appsettings file was properly added to the configuration. Also checked that the environment in the Startup Configure
method was either Development or Production but still ending up with the same old port 5000 which belongs to none of my appsettings files.
[Edit] Possible in .NET 2.1
See: https://github.com/aspnet/KestrelHttpServer/issues/1290#issuecomment-353416087