你的位置:首页 > ASP.net教程

[ASP.net教程]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": true48  },49 50  "runtimeOptions": {51   "configProperties": {52    "System.GC.Server": true53   }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.Server.Kestrel.Https; 7 using Microsoft.AspNetCore.Hosting; 8  9 namespace Demo10 {11   public class Program12   {13     public static void Main(string[] args)14     {15 16       var host = new WebHostBuilder()17         .UseKestrel()18         .UseUrls("http://*", "https://*")19         .UseContentRoot(Directory.GetCurrentDirectory())20         .UseIISIntegration()21         .UseStartup<Startup>()22         .Build();23 24       host.Run();25     }26   }27 }

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 Demo14 {15   public class Startup16   {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(new System.Security.Cryptography.X509Certificates.X509Certificate2(38           Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, "cret.pfx"), "certPassword"));39       });40 41     }42 43     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.44     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)45     {46       loggerFactory.AddConsole(Configuration.GetSection("Logging"));47       loggerFactory.AddDebug();48 49       if (env.IsDevelopment())50       {51         app.UseDeveloperExceptionPage();52         app.UseBrowserLink();53       }54       else55       {56         app.UseExceptionHandler("/Home/Error");57       }58 59 60       app.UseStaticFiles();61 62       app.UseMvc(routes =>63       {64         routes.MapRoute(65           name: "default",66           template: "{controller=App}/{action=Index}/{id?}");67       });68 69       //https://docs.asp.net/en/latest/security/cors.html 跨域70       app.UseCors(builder =>builder.WithOrigins("https://*").AllowAnyHeader());71 72       app.Run(run =>73       {74         return run.Response.WriteAsync("Test");75       });76 77     }78   }79 }