ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)_實用技巧

來源:互聯網
上載者:User

最近要做一個項目,正逢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

{ "dependencies": { //跨平台引用 //"Microsoft.NETCore.App": { // "version": "1.0.0", // "type": "platform" //}, "Microsoft.AspNetCore.Diagnostics": "1.0.0", "Microsoft.AspNetCore.Mvc": "1.0.0", "Microsoft.AspNetCore.Razor.Tools": {  "version": "1.0.0-preview2-final",  "type": "build" }, "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel.Https": "1.0.0", "Microsoft.AspNetCore.StaticFiles": "1.0.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", "Microsoft.Extensions.Logging": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0" }, "tools": { "BundlerMinifier.Core": "2.0.238", "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final", "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" }, "frameworks": { //跨平台引用 //"netcoreapp1.0": { // "imports": [ // "dotnet5.6", // "portable-net45+win8" // ] //} //Windows平台通用化引用 "net452": {} }, "buildOptions": { "emitEntryPoint": true, "preserveCompilationContext": true }, "runtimeOptions": { "configProperties": {  "System.GC.Server": true } }, "publishOptions": { "include": [  "wwwroot",  "Views",  "Areas/**/Views",  "appsettings.json",  "web.config" ], "exclude": [  "wwwroot/lib" ] }, "scripts": { "prepublish": [ "bower install", "dotnet bundle" ], "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] }}

在Program.cs中,增加HTTPS訪問連接埠綁定

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Hosting;namespace Demo{ public class Program {  public static void Main(string[] args)  {   var host = new WebHostBuilder()    .UseKestrel()    .UseUrls("http://*", "https://*")    .UseContentRoot(Directory.GetCurrentDirectory())    .UseIISIntegration()    .UseStartup<Startup>()    .Build();   host.Run();  } }}

在 Startup.cs 檔案中,啟用HTTPS訪問並配置憑證路徑及密碼

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using System.IO;using Microsoft.AspNetCore.Http;namespace Demo{ public class Startup {  public Startup(IHostingEnvironment env)  {   var builder = new ConfigurationBuilder()    .SetBasePath(env.ContentRootPath)    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)    .AddEnvironmentVariables();   Configuration = builder.Build();  }  public IConfigurationRoot Configuration { get; }  // 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();   services.Configure<Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions>(option => {    option.UseHttps(Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, "cret.pfx"), "pw");   });  }  // 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)  {   loggerFactory.AddConsole(Configuration.GetSection("Logging"));   loggerFactory.AddDebug();   if (env.IsDevelopment())   {    app.UseDeveloperExceptionPage();    app.UseBrowserLink();   }   else   {    app.UseExceptionHandler("/Home/Error");   }   app.UseStaticFiles();   app.UseMvc(routes =>   {    routes.MapRoute(     name: "default",     template: "{controller=App}/{action=Index}/{id?}");   });   //https://docs.asp.net/en/latest/security/cors.html?highlight=https   app.UseCors(builder =>builder.WithOrigins("https://*").AllowAnyHeader());   app.Run(run =>   {    return run.Response.WriteAsync("Test");   });  } }}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.