如何在.Net Core 2.1中配置用于登台/生产的SSL

时间:2018-08-08 10:26:27

标签: c# asp.net-core-2.1

我正在使用.NET Core 2.1构建mvc Web应用程序。在.net核心的ListenOptions.UseHttps下的文档中,该文档指出它将为Kestrel配置默认证书(doc found here)。

Kestrel在哪里寻找此默认证书?我如何替换它进行登台(我想在其中使用我们的演示站点证书之一)?并用于生产(我再次拥有其他证书)?

我的Program.cs内容现在看起来像:

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();
    }
}

我的Startup.cs配置方法

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);
}

应用程序在本地主机上使用https可以正常运行。我正在寻找一种通过配置(例如appSettings。{env} .json?)而不是通过自定义方法(例如this one

)解决此问题的方法

1 个答案:

答案 0 :(得分:2)

xneg提到的文档帮助解决了这一问题:Kestrel web server implementation in ASP.NET Core

设法通过如下修改appsettings.DockerDevelopment.json文件使其工作:

if game.score - game.scoreAtLastColorChange >= 10 {
    game.changeColors()
    game.scoreAtLastColorChange = game.score
}

Kestrel现在可以在Docker容器中和登台服务器上使用https来运行我的应用程序。