.NET 部署 多域名 Https(SSL)通過代碼方式
在上一個文章中,傳送門,給大家介紹了怎么在配置文件中使用 Kestrel 部署 Https,正好今天有小伙伴穩問到:可以通過代碼的方式實現 Kestrel 的 Https 的部署嗎?答案是肯定的,我們這次一樣去不是多個域名。
在使用代碼實現中,我是主要使用到 ListenOptions.UseHttps,我們先看看官方文檔怎么說吧,不想看我的可以直接跳轉到官方文檔,傳送門
ListenOptions.UseHttps
將 Kestrel 配置為使用 HTTPS。
ListenOptions.UseHttps 擴展:
UseHttps:將 Kestrel 配置為使用 HTTPS,采用默認證書。 如果沒有配置默認證書,則會引發異常。UseHttps(string fileName)UseHttps(string fileName, string password)UseHttps(string fileName, string password, Action<HttpsConnectionAdapterOptions> configureOptions)UseHttps(StoreName storeName, string subject)UseHttps(StoreName storeName, string subject, bool allowInvalid)UseHttps(StoreName storeName, string subject, bool allowInvalid, StoreLocation location)UseHttps(StoreName storeName, string subject, bool allowInvalid, StoreLocation location, Action<HttpsConnectionAdapterOptions> configureOptions)UseHttps(X509Certificate2 serverCertificate)UseHttps(X509Certificate2 serverCertificate, Action<HttpsConnectionAdapterOptions> configureOptions)UseHttps(Action<HttpsConnectionAdapterOptions> configureOptions)
ListenOptions.UseHttps 參數:
filename是證書文件的路徑和文件名,關聯包含應用內容文件的目錄。password是訪問 X.509 證書數據所需的密碼。configureOptions是配置HttpsConnectionAdapterOptions的Action。 返回ListenOptions。storeName是從中加載證書的證書存儲。subject是證書的主題名稱。allowInvalid指示是否存在需要留意的無效證書,例如自簽名證書。location是從中加載證書的存儲位置。serverCertificate是 X.509 證書。
在生產中,必須顯式配置 HTTPS。 至少必須提供默認證書。
下面要描述的支持的配置:
- 無配置
- 從配置中替換默認證書
- 更改代碼中的默認值
無配置
Kestrel 在 http://localhost:5000 和 https://localhost:5001 上進行偵聽(如果默認證書可用)。
從配置中替換默認證書
Kestrel 可以使用默認 HTTPS 應用設置配置架構。 從磁盤上的文件或從證書存儲中配置多個終結點,包括要使用的 URL 和證書。
架構的注意事項:
- 終結點的名稱不區分大小寫。 例如,由于再也無法解析標識符“Families”,因此
HTTPSandHttps是等效的。 - 每個終結點都要具備
Url參數。 此參數的格式和頂層Urls配置參數一樣,只不過它只能有單個值。 - 這些終結點不會添加進頂層
Urls配置中定義的終結點,而是替換它們。 通過Listen在代碼中定義的終結點與在配置節中定義的終結點相累積。 Certificate部分是可選的。 如果未指定Certificate部分,則使用Certificates:Default中定義的默認值。 如果沒有可用的默認值,則使用開發證書。 如果沒有默認值,且開發證書不存在,則服務器將引發異常,并且無法啟動。Certificate部分支持多個證書源。- 只要不會導致端口沖突,就能在配置中定義任何數量的終結點。
證書源
可以將證書節點配置為從多個源加載證書:
Path和Password用于加載 .pfx 文件。Path、KeyPath和Password用于加載 .pem/.crt 和 .key 文件。Subject和Store用于從證書存儲中加載。
好了,羅嗦話說完了,我們抽取文檔的一部分進行實踐
var builder = WebApplication.CreateBuilder(args); builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.ListenAnyIP(5005, listenOptions => { listenOptions.UseHttps(httpsOptions => { var localhostCert = CertificateLoader.LoadFromStoreCert( "localhost", "My", StoreLocation.CurrentUser, allowInvalid: true); var exampleCert = CertificateLoader.LoadFromStoreCert( "example.com", "My", StoreLocation.CurrentUser, allowInvalid: true); var subExampleCert = CertificateLoader.LoadFromStoreCert( "sub.example.com", "My", StoreLocation.CurrentUser, allowInvalid: true); var certs = new Dictionary<string, X509Certificate2>( StringComparer.OrdinalIgnoreCase) { ["localhost"] = localhostCert, ["example.com"] = exampleCert, ["sub.example.com"] = subExampleCert }; httpsOptions.ServerCertificateSelector = (connectionContext, name) => { if (name is not null && certs.TryGetValue(name, out var cert)) { return cert; } return exampleCert; }; }); }); });
上面的代碼一看就能懂,比較無奈的是官方文檔的 SSL 證書是從 證書存儲區 里獲取的,在實際應用中,明顯是不夠方便,最好是那種直接寫 證書路徑 和 密碼的,這樣才能一目了然嘛,而這里的關鍵就是 X509Certificate2 這個類了,可以看到,最終是通過檢索一個字典返回的,接受的就是這個 X509Certificate2 類,所以我們看看這個類到底是個什么東西,傳送門;
這里我們只關注構造函數,下面是官方文檔,或者直接 F12 進去看更為直接
構造函數
直接 F12 也貼出來吧,方便大伙查看

帥氣的小伙伴可能已經發現了,里面就存在了一個 直接傳入 文件路徑 和 密碼 作為參數的構造函數,毫無疑問,它就是我們要找的?。?!下面我們直接看代碼:
Program.cs
builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.ConfigureHttpsDefaults(listenOptions => { listenOptions.SslProtocols = SslProtocols.Tls13; }); serverOptions.ListenAnyIP(5209, listenOptions => { listenOptions.UseHttps(httpsOptions => { var test1 = new X509Certificate2("cer\\test1.ysmc.net.cn_server.pfx", "密碼1"); var test2 = new X509Certificate2("cer\\test2.ysmc.net.cn_server.pfx", "密碼2"); var certs = new Dictionary<string, X509Certificate2>( StringComparer.OrdinalIgnoreCase) { ["test1.ysmc.net.cn"] = test1, ["test2.ysmc.net.cn"] = test2 }; httpsOptions.ServerCertificateSelector = (connectionContext, name) => { if (name is not null && certs.TryGetValue(name, out var cert)) { return cert; } return test1; }; }); }); });

因為是配合了 YARP “食用”的,詳情可以查看我前面的文章,傳送門,所以不同的域名會反向代理到不同的網站上面,好了,文章到此結束,感謝大佬們的閱讀,謝謝!
本文來自博客園,作者:一事冇誠,轉載請注明原文鏈接:http://www.rzrgm.cn/ysmc/p/16721268.html

浙公網安備 33010602011771號