Kestrel使用Https证书

public class Program
    {
        private static AppSettings _appSettings { get; } = new ConfigurationBuilder()
                        .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                        .AddJsonFile("appsettings.json", false)
        #if DEBUG
                                .AddJsonFile("appsettings.Development.json", true)
        #endif
                                .Build().Get<AppSettings>();

        public static async Task<int> Main(string[] args)
        {
            var logger = LogManager.GetCurrentClassLogger();
            try
            {
                Console.WriteLine("Starting host...");
                var host = CreateHostBuilder(args).Build();
                Console.WriteLine($"{string.Join("\r\n", _appSettings.Urls)}\r\n");
                await host.RunAsync();
                //CreateHostBuilder(args).Build().Run();
                return 0;
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Stopped program because of exception");
                return 1;
            }
            finally
            {
                LogManager.Shutdown();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                var certpath = Path.Combine(AppContext.BaseDirectory, _appSettings.Certificate.Path);
                var certificate = new X509Certificate2(certpath, _appSettings.Certificate.Password);
                webBuilder
                //.UseEnvironment(Environments.Production)
                .UseStartup<Startup>()
                .UseUrls(_appSettings.Urls)//多URL
                .UseKestrel(options =>
                {
                    options.ConfigureHttpsDefaults(options => { options.ServerCertificate = certificate; });
                })
                ;
            })
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .UseNLog();
    }
上一篇:一文讲通.NET Core部署到Windows IIS最全解决方案


下一篇:Kestrel.Transport.Sockets分析与使用