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

 

 

 

截圖:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM