Search code examples
c#asp.net-core-2.1

How to configure ssl for Staging/Production in .Net Core 2.1


I am building an mvc web app using .NET Core 2.1. In the documentation on .net core under ListenOptions.UseHttps it states it will configure Kestrel with the default certificate (doc found here).

Where does Kestrel look for this default certificate? How can I replace it for staging (where I would want to use one of our demo site certificates)? And for production (where I have a different cert again)?

My Program.cs content now looks like:

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebHost.CreateDefaultBuilder(args); //This sets up Kestrel and adds appsettings.json to the configuration
        builder.UseStartup<Startup>();
        builder.ConfigureAppConfiguration((context, configurationBuilder) =>
        {
            var env = context.HostingEnvironment;
            configurationBuilder.SetBasePath(env.ContentRootPath);
        });

        var host = builder.Build();
        host.Run();
    }
}

My Startup.cs Configure method

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseMvc(ConfigureRoutes);
}

The application runs fine with https on localhost. I am looking for a way to solve this via configuration (e.g. appSettings.{env}.json?) and not via custom methods such as this one


Solution

  • The documentation mentioned by xneg helped solve this: Kestrel web server implementation in ASP.NET Core

    Managed to get it to work by modifying the appsettings.DockerDevelopment.json file as follows:

    {
      "Kestrel": {
        "EndPoints": {
          "HttpsDefaultCert": {
            "Url": "https://*:443"
          },
          "Http": {
            "Url": "http://*:80"
          }
        },
        "Certificates": {
          "Default": {
            "Path": "certifcate.pfx",
            "Password": "*********"
          }
        }
      }
    }
    

    Kestrel is now able to run my app using https in a Docker container and on the staging server.