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,就完成了

 

posted @ 2022-03-14 20:30  中国结  阅读(469)  评论(0编辑  收藏  举报