如何使用自託管 ASP.NET Core 2 應用程序 (httpsys) 進行 HTTPS (SSL)
我寫了一個小的 ASP.NET Core 2 應用程序。它作為服務執行,因此沒有 IIS。它在裝有 Windows 7 SP1 的 PC 上執行。
var host = WebHost.CreateDefaultBuilder(args) .UseContentRoot(pathToContentRoot) .UseHttpSys(options => { options.Authentication.Schemes = AuthenticationSchemes.None; options.Authentication.AllowAnonymous = true; options.MaxConnections = null; options.MaxRequestBodySize = 30000000; options.UrlPrefixes.Add("http://*:5050"); }) .UseStartup<Startup>() .UseApplicationInsights() .Build(); if (isService) { host.RunAsService(); } else { host.Run(); }如您所見,我想在埠 5050 上監聽。沒有 SSL 也可以正常工作。
我的問題是,如何為我的應用程序啟用 https?再次:沒有 IIS,沒有域名(沒有網際網路連接)。通信只是在內部網路內部,所以我想使用自簽名證書。
我閱讀了文件(HTTP.sys 文件;Netsh 命令;New-SelfSignedCertificate),但總有一些與我的情況不同的地方(他們使用 Krestel,或者是使用 IIS)。另外,我不知道如何為我的應用程序獲取 App-ID(netsh 需要)。我試過這個:StackOverflow Get GUID但它不起作用。
var assembly = typeof(Program).Assembly; // following line produces: System.IndexOutOfRangeException var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; var id = attribute.Value; Console.WriteLine(id);所以我對所有的可能性和不同的配置有點困惑。而且文件沒有考慮我的具體情況。
我創建了一個證書,我想我需要將它儲存在“我的”商店中。(那在哪裡?cert:\LocalMachine\My)然後我需要為其分配我的 Applicaion ID 和埠。
但我不知道該怎麼做。任何人都可以幫忙嗎?
所以我通過以下方式解決問題:
首先,如果您想知道自己的 GUID,您將使用以下程式碼獲得它:
var id = typeof(RuntimeEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<GuidAttribute>().Value;創建自簽名證書
現在創建一個 SelfSigned-Certificate(如果您已經擁有或購買了,請跳過此步驟)
- 執行以下 OpenSSL 命令以生成您的私鑰和公共證書。回答問題並在出現提示時輸入通用名稱。
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem
- 將您的密鑰和證書組合到一個 PKCS#12 (P12) 包中:
openssl pkcs12 -inkey key.pem -in certificate.pem -export -out certificate.p12
在客戶端安裝證書:
對於 Windows 8 及更高版本:
使用PowerShell將證書添加到 Windows 證書儲存區
PS C:> $certpwd = ConvertTo-SecureString -String “passwort” -Force –AsPlainText
PS C:> Import-PfxCertificate –FilePath D:\data\cert\certificate.p12 cert:\localMachine\my -Password $certpwd
獲取證書的指紋(雜湊)
PS C:\WINDOWS\system32> 目錄 Cert:\LocalMachine\my
安裝證書(用您的值替換雜湊、IP 和埠)
PS C:\WINDOWS\system32> $guid =
$$ guid $$::NewGuid() PS C:\WINDOWS\system32> $certHash = “A1D…B672E”
PS C:\WINDOWS\system32> $ip = “0.0.0.0”
PS C:\WINDOWS\system32> $port = “5050”
PS C:\WINDOWS\system32> “http 添加 sslcert ipport= $ ( $ ip): $ port certhash= $ certHash appid={$guid}” | netsh
你完成了。
對於 Windows 7
將證書添加到 Windows Cert Store (注意:此操作使用 .pem 文件,因為 certutil 似乎不支持 .p12 文件)
.\certutil.exe -addstore -enterprise -f “Root” C:\lwe\cert\certificate.pem
如果他的行拋出以下錯誤:
SSL 證書添加失敗,錯誤 1312 指定的登錄會話不存在。它可能已經被終止。
您必須手動執行這些步驟(手動執行時請插入 .p12 文件,而不是 .pem):
執行 mmc.exe
- 轉到文件-> 添加/刪除管理單元
- 選擇證書管理單元。
- 選擇電腦帳戶
- 導航到:證書(本地電腦)\個人\證書
- 右鍵點擊證書文件夾並選擇所有任務 -> 導入。
- 按照嚮導說明選擇證書。確保在嚮導期間選中導出複選框。
要獲取您證書的雜湊值,請執行 Internet Explorer,按 Alt + X 並轉到 Internet 選項 -> 內容 -> 證書。搜尋您的證書並閱讀雜湊。
現在您可以執行與 Windows 8+ 相同的命令:
安裝證書(用您的值替換雜湊、IP 和埠)
PS C:\WINDOWS\system32> $guid =
$$ guid $$::NewGuid() PS C:\WINDOWS\system32> $certHash = “A1D…B672E”
PS C:\WINDOWS\system32> $ip = “0.0.0.0”
PS C:\WINDOWS\system32> $port = “5050”
PS C:\WINDOWS\system32> “http 添加 sslcert ipport= $ ( $ ip): $ port certhash= $ certHash appid={$guid}” | netsh
編輯您的程式碼
畢竟,您必須將 UrlPrefixes 設置為 https。因此,在您的 Program.cs 文件中,您需要:
var host = WebHost.CreateDefaultBuilder(args) .UseContentRoot(pathToContentRoot) .UseHttpSys(options => { options.Authentication.Schemes = AuthenticationSchemes.None; options.Authentication.AllowAnonymous = true; options.MaxConnections = null; options.MaxRequestBodySize = 30000000; options.UrlPrefixes.Add("https://*:5050"); }) .UseStartup<Startup>() .UseApplicationInsights() .Build();