我在 ubuntu 20.04 上有一個 ASP.NET Web 應用程式,我正在使用.pfx格式的 SSL 證書,效果很好。但是,我想學習如何對.pem檔案執行相同的操作。
我知道它可以appsettings.json像這樣通過HttpsFromPem密鑰完成:
{
"Kestrel": {
"Endpoints": {
"HttpsInlineCertAndKeyFile": {
"Url": "https://localhost:5001",
"Certificate": {
"Path": "<path to .pem/.crt file>",
"KeyPath": "<path to .key file>",
"Password": "$CREDENTIAL_PLACEHOLDER$"
}
}
}
}
}
我知道如何使用這樣的.pfx格式:
var httpsCert = Environment.GetEnvironmentVariable("HTTPS_CERT");
var httpsCertKey = Environment.GetEnvironmentVariable("HTTPS_CERT_KEY");
if (httpsCert != null && httpsCertKey != null)
{
options.Listen(IPAddress.Loopback, 5001,
listenOptions => listenOptions.UseHttps(httpsCert, httpsCertKey));
}
來源 - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0
我的問題是:如何配置 Kestrel 以從.pem代碼檔案中讀取證書?
uj5u.com熱心網友回復:
您可以將其加載到使用中
var pemPath = //read in from configuration
var privateKeyPath = //read in from configuration
var certificate = X509Certificate2.CreateFromPemFromFile(pemPath, privateKeyPath);
然后,您可以在配置 Kestrel 時使用類似的內容配置 Kestrel。
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.ConfigureHttpsDefaults(adapterOptions =>
{
adapterOptions.ServerCertificate = certificate
});
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446525.html
