Core 1.0 部署 HTTPS (Core 1.0)

2018-09-06 12:33

阅读:431

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

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

  aspnetcore-using-https-on-dnx451.html?utm_source=tuicoolutm_medium=referral

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

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

1 { 2 "dependencies": { 3 //跨平台引用ore.App": { 5 // "version": "1.0.0", 6 // "type": "platform" 7 //}, 8 "Microsoft.AspNetCore.Diagnostics": "1.0.0", 9 "Microsoft.AspNetCore.Mfiguration.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 " project.json

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

() 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


评论


亲,登录后才可以留言!