close
文章出處

在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
作者博客:Savorboard
歡迎轉載,請在明顯位置給出出處及鏈接


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()