ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)¶
警告
您當前查看的頁面是未經授權的轉載!
如果當前版本排版錯誤,請前往查看最新版本:http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html
提示
更新時間: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訪問也是很簡單,只需要勾選一下即可:

對應的 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 處獲得。