zoukankan      html  css  js  c++  java
  • ASP.NET Core 1.0 部署 HTTPS

    ASP.NET Core 1.0 部署 HTTPS 

    ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)

    提示

    更新时间:2016年01月23日。

    在目前介绍 ASP.NET Core 1.0 的中英文文章中,我没有找到关于部署HTTPS的, 究其原因,除了暂时无法跨平台外,就算是很少人有这个需求,但我还是决定写一下。

    警告

    目前( 1.0.0-rc1-update1 )仅支持完整版的dnx451,dnxcore5需要rc2的支持。目前只能运行在Windows上。

    指定了运行时,部署HTTPS还需要根据不同的Web服务器(IIS/Kestrel)有不同的配置方案。

    IIS

    这个的配置和传统的 ASP.NET 4.6程序一样,代码无需更改,只要在指定域名的时候配置证书即可。

    IIS Express

    IIS Express 是供调试时使用的精简版,仅能通过域名 localhost 访问到。 启用HTTPS访问也是很简单,只需要勾选一下即可:

    https://blogs.blob.core.chinacloudapi.cn/images/iis-express-enable-ssl.png

    对应的 launchSettings.json 多了一行:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:14842/",
          "sslPort": 44362
        }
      },
    

    建议把此证书添加到受信任的根证书颁发机构中来避免浏览器的错误提示。

    Kestrel

    不同于IIS,Kestrel没有管理平台,需要在代码中配置。

    提示

    即使进行了下面的更改,依然可以使用IIS来运行,但更改并不会在IIS上生效。 本代码仅适用于 1.0.0-rc1-final /1.0.0-rc1-update1

    在 project.json 中,进行如下操作:

    • 删除 dnxcore5,仅保留 dnx451
    • 添加引用 Microsoft.AspNet.Server.Kestrel.Https (仅支持.NET 4.5.1)
    • 增加HTTPS访问端口(本例44300)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    {
      "version": "1.0.0-*",
      "compilationOptions": {
        "emitEntryPoint": true
      },
    
      "dependencies": {
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel.Https": "1.0.0-rc1-final",
        "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"
      },
    
      "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://*:8000;https://*:44300"
      },
    
      "frameworks": {
        "dnx451": { }
      },
    
      "exclude": [
        "wwwroot",
        "node_modules"
      ],
      "publishExclude": [
        "**.user",
        "**.vspscc"
      ]
    }
    

    同时,在 Startup.cs 文件中,也需要添加如下配置: (其中第40,43行包含了SSL证书的路径和密码(敏感信息不建议硬编码))

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    using Microsoft.AspNet.Builder;
    using Microsoft.AspNet.Hosting;
    using Microsoft.AspNet.Server.Kestrel.Https;
    using System.Security.Cryptography.X509Certificates;
    using System.IO;
    
    namespace Dnx451HttpsDemo
    {
        public class Startup
        {
            public Startup(IHostingEnvironment env)
            {
                // Set up configuration sources.
                var builder = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json")
                    .AddEnvironmentVariables();
                Configuration = builder.Build();
            }
    
            public IConfigurationRoot Configuration { get; set; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                // Add framework services.
                services.AddMvc();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                app.UseIISPlatformHandler();
    
                app.UseStaticFiles();
    
                app.UseMvc();
    
                var cretPath = Path.Combine(
                    new DirectoryInfo(env.MapPath("")).Parent.FullName, 
                        "localhost.pfx");
                        
                app.UseKestrelHttps(
                    new X509Certificate2(cretPath, "certPassword"));
            }
    
            // Entry point for the application.
            public static void Main(string[] args) => WebApplication.Run<Startup>(args);
        }
    }
    

    目前,我们就可以通过 https://localhost:44300 访问Kestrel服务器了。

    总结

    在Windows上,运行时可以用dnx451和dnxcore5;Web服务器可用IIS和Kestrel。 目前跨平台的dnxcore5不支持HTTPS(需要 ASP.NET Core rc2的支持),而且可以肯定的是代码又有大改。

    ASP.NET Core 1.0 并不是像微软说的一样,RC可以使用了。 ASP.NET Core 还有很多不完善,如短期内还不支持 HTTP/2 等。

    声明 知识共享许可协议

    ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1) 由 勤奋的小孩 创作,采用 知识共享 署名 4.0 国际 许可协议进行许可。
    本许可协议授权之外的使用权限可以从 http://space.cnblogs.com/msg/send/qin-nz 处获得。

  • 相关阅读:
    磁盘拓展
    echarts遇到resize失效问题
    技术的技术书籍
    beego的数据库操作优化
    Laravel学习笔记(六)数据库 数据库填充
    Laravel学习笔记(五)数据库 数据库迁移案例2——创建数据结构,数据表,修改数据结构
    Laravel学习笔记(四)数据库 数据库迁移案例
    Laravel学习笔记(二)Laravel 应用程序的体系结构
    Laravel学习笔记(三)数据库 数据库迁移
    Laravel安装配置开发环境
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5153717.html
Copyright © 2011-2022 走看看