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

时间:2020-03-30 14:56:41   收藏:0   阅读:497

技术分享图片

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

 

 

 

截图:

 

技术分享图片

 

技术分享图片

 

原文:https://www.cnblogs.com/datacool/p/12598307.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!