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

在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即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值