1. 程式人生 > 其它 >ASP.NET Core Kestrel部署HTTPS

ASP.NET Core Kestrel部署HTTPS

ASP.NET Core Kestrel部署HTTPS(阿里雲ssl證書)

1.阿里雲下載IIS證書(pfx),放到專案根目錄下

2、改程式碼

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                   .UseStartup
<Startup>()
            //加入以下程式碼 .UseKestrel(options
=>//設定Kestrel伺服器 { options.Listen(IPAddress.Any, 443, listenOptions => { //填入之前iis中生成的pfx檔案路徑和指定的密碼 listenOptions.Protocols = HttpProtocols.Http1; listenOptions.UseHttps(
"xxxx.pfx", "證書的密碼"); }); })
            //結束 .ConfigureLogging(logging
=> { logging.ClearProviders(); logging.SetMinimumLevel(LogLevel.Trace); }).UseNLog(); }

3、http重定向到HTTPS(開80/443埠)

  IIS中新建一個80埠的網站,只有一個index.html檔案,內容如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>  
<!-- 以下方式定時轉到其他頁面 -->  
<meta http-equiv="refresh" content="0; url=https://你的域名">

</head>
</html>

然後在瀏覽器輸入:你的域名(不含http)回車後,自動轉到HTTPS,就完成了