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

这两个月要做一个项目,正逢ASP.Net Core 1.0版本的正式发布。由于现代互联网的安全要求,HTTPS加密通讯已成主流,所以就有了这个方案。

本方案启发于一个旧版的解决方案:

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

http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html?utm_source=tuicool&utm_medium=referral

在反复搜索官方文档并反复尝试以后得出以下解决方案

 

project.json 中,添加引用 Microsoft.AspNetCore.Server.Kestrel.Https

 1 {
 2   "dependencies": {
 3     //跨平台引用
 4     //"Microsoft.NETCore.App": {
 5     //  "version": "1.0.0",
 6     //  "type": "platform"
 7     //},
 8     "Microsoft.AspNetCore.Diagnostics": "1.0.0",
 9     "Microsoft.AspNetCore.Mvc": "1.0.0",
10     "Microsoft.AspNetCore.Razor.Tools": {
11       "version": "1.0.0-preview2-final",
12       "type": "build"
13     },
14     "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
15     "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
16     "Microsoft.AspNetCore.Server.Kestrel.Https": "1.0.0",
17     "Microsoft.AspNetCore.StaticFiles": "1.0.0",
18     "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
19     "Microsoft.Extensions.Configuration.Json": "1.0.0",
20     "Microsoft.Extensions.Logging": "1.0.0",
21     "Microsoft.Extensions.Logging.Console": "1.0.0",
22     "Microsoft.Extensions.Logging.Debug": "1.0.0",
23     "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
24     "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
25   },
26 
27   "tools": {
28     "BundlerMinifier.Core": "2.0.238",
29     "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
30     "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
31   },
32 
33   "frameworks": {
34     //跨平台引用
35     //"netcoreapp1.0": {
36     //  "imports": [
37     //    "dotnet5.6",
38     //    "portable-net45+win8"
39     //  ]
40     //}
41     //Windows平台通用化引用
42     "net452": {}
43   },
44 
45   "buildOptions": {
46     "emitEntryPoint": true,
47     "preserveCompilationContext": true
48   },
49 
50   "runtimeOptions": {
51     "configProperties": {
52       "System.GC.Server": true
53     }
54   },
55 
56   "publishOptions": {
57     "include": [
58       "wwwroot",
59       "Views",
60       "Areas/**/Views",
61       "appsettings.json",
62       "web.config"
63     ],
64     "exclude": [
65       "wwwroot/lib"
66     ]
67   },
68 
69   "scripts": {
70     "prepublish": [ "bower install", "dotnet bundle" ],
71     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
72   }
73 }
project.json

 在Program.cs中,增加HTTPS访问端口绑定

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Hosting;
 7 
 8 namespace Demo
 9 {
10     public class Program
11     {
12         public static void Main(string[] args)
13         {
14 
15             var host = new WebHostBuilder()
16                 .UseKestrel()
17                 .UseUrls("http://*", "https://*")
18                 .UseContentRoot(Directory.GetCurrentDirectory())
19                 .UseIISIntegration()
20                 .UseStartup<Startup>()
21                 .Build();
22 
23             host.Run();
24         }
25     }
26 }
Program.cs

Startup.cs 文件中,启用HTTPS访问并配置证书路径及密码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Builder;
 6 using Microsoft.AspNetCore.Hosting;
 7 using Microsoft.Extensions.Configuration;
 8 using Microsoft.Extensions.DependencyInjection;
 9 using Microsoft.Extensions.Logging;
10 using System.IO;
11 using Microsoft.AspNetCore.Http;
12 
13 namespace Demo
14 {
15     public class Startup
16     {
17         public Startup(IHostingEnvironment env)
18         {
19             var builder = new ConfigurationBuilder()
20                 .SetBasePath(env.ContentRootPath)
21                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
22                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
23                 .AddEnvironmentVariables();
24             Configuration = builder.Build();
25         }
26 
27         public IConfigurationRoot Configuration { get; }
28 
29         // This method gets called by the runtime. Use this method to add services to the container.
30         public void ConfigureServices(IServiceCollection services)
31         {
32 
33             // Add framework services.
34             services.AddMvc();
35 
36             services.Configure<Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions>(option => { 37                 option.UseHttps(Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, "cret.pfx"), "pw"); 38  }); 39 
40 
41 
42         }
43 
44         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
45         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
46         {
47             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
48             loggerFactory.AddDebug();
49 
50             if (env.IsDevelopment())
51             {
52                 app.UseDeveloperExceptionPage();
53                 app.UseBrowserLink();
54             }
55             else
56             {
57                 app.UseExceptionHandler("/Home/Error");
58             }
59 
60 
61             app.UseStaticFiles();
62 
63             app.UseMvc(routes =>
64             {
65                 routes.MapRoute(
66                     name: "default",
67                     template: "{controller=App}/{action=Index}/{id?}");
68             });
69 
70             //https://docs.asp.net/en/latest/security/cors.html?highlight=https
71             app.UseCors(builder =>builder.WithOrigins("https://*").AllowAnyHeader());
72 
73             app.Run(run =>
74             {
75                 return run.Response.WriteAsync("Test");
76             });
77 
78         }
79     }
80 }

 

你可能感兴趣的:(ASP.NET Core 1.0 部署 HTTPS (.NET Core 1.0))