Kestrel服务器ASP.NetCore 3.1程序启用SSL

VS2019向导建立的MVC、web api类型的解决方案只要勾选启用SSL(https)使用IIS Express来调试程序时VS自动就给我们配置好了,项目启用SSL。我们这次使用IIS自动生成的本地证书来完成Kestrel启用SSL。

生成pfx证书

开发环境证书就用iis默认的本地证书即可,

进入管理器:点击服务器证书选项

选中以下本地默认证书后右键导出,指定路径和密码点击确认.

 

 

 代码:

 

 public class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                Console.WriteLine(DateTime.Now + "->CommandLine Args:" + string.Join("|", args));
            }
            CreateHostBuilder(args).Build().Run(); 
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(builder =>
                {
                    builder.AddCommandLine(args);
                }) 
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                              .UseKestrel(options =>
                               {
                                   options.Listen(IPAddress.Loopback, 443, listenOptions =>
                                   {
                                       listenOptions.UseHttps(AppDomain.CurrentDomain.BaseDirectory + "datacool.pfx", "password");
                                   });
                               });
                });
    }

 

 

 

截图:

 

 

 

posted @ 2020-03-30 14:13  数据酷软件  阅读(966)  评论(0编辑  收藏  举报