在 ASP.NET 6.0 应用程序中使用 .pem 文件而不是 .pfx 配置 ListenOptions.UseHttps
我在 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));
}
我的问题是:如何配置 Kestrel 来读取证书代码中的 .pem
文件?
I have an ASP.NET web app on ubuntu 20.04, and I am using SSL certificate in .pfx
format, which works fine. However, I want to learn how to do the same with the .pem
file.
I know it can be done in appsettings.json
like this and through the HttpsFromPem
key:
{
"Kestrel": {
"Endpoints": {
"HttpsInlineCertAndKeyFile": {
"Url": "https://localhost:5001",
"Certificate": {
"Path": "<path to .pem/.crt file>",
"KeyPath": "<path to .key file>",
"Password": "$CREDENTIAL_PLACEHOLDERquot;
}
}
}
}
}
And I know how to use the .pfx
format like so:
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));
}
My question is: how to configure Kestrel to read cert from the .pem
file in code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用加载它
然后您可以在配置 Kestrel 时使用类似的内容配置 Kestrel。
You can just load it in using
Then you can configure Kestrel with something like this when you're configuring Kestrel.
.NET 7
X509Certificate2
有一个方法CreateFromPemFile(certPemFilePath, keyPemFilePath)
:.NET 7
X509Certificate2
has a methodCreateFromPemFile(certPemFilePath, keyPemFilePath)
: