ASP.NET Core Kestrel 中使用 HTTPS (SSL)

dotNET跨平臺發表於2016-08-18

在ASP.NET Core中,如果在Kestrel中想使用HTTPS對站點進行加密傳輸,可以按照如下方式

申請證書

這一步就不詳細說了,有免費的和收費的,申請完成之後會給你一個*.pfx結尾的檔案。

新增NuGet包

nuget中查詢然後再程式中新增引用Microsoft.AspNetCore.Server.Kestrel.Https

配置

*.pfx結尾的檔案拷貝的程式的Web根目錄,然後修改Programs.cs檔案:

public class Program{       
  public static void Main(string[] args)
 
{          
      var config = new ConfigurationBuilder().AddCommandLine(args).AddEnvironmentVariables("ASPNETCORE_").Build();      
       var host =   new WebHostBuilder().UseConfiguration(config).UseKestrel(ConfigHttps()).UseContentRoot( Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup<Startup>().Build();                        host.Run();        }      
       
       private static Action<KestrelServerOptions> ConfigHttps() {            return x => {                var pfxFile = Path.Combine(Directory.GetCurrentDirectory(), "*.pfx");                //password 填寫申請的金鑰                var certificate = new X509Certificate2(pfxFile, "password");                x.UseHttps(certificate);            };        }    }

然後命令列視窗執行dotnet xxx.dll --server.urls https://www.example.com:port即可。


原文地址:http://www.cnblogs.com/savorboard/p/aspnetcore-kestrel-https.html


.NET社群新聞,深度好文,微信中搜尋dotNET跨平臺或掃描二維碼關注

ASP.NET Core Kestrel 中使用 HTTPS (SSL)

相關文章