在 ASP.NET 6.0 应用程序中使用 .pem 文件而不是 .pfx 配置 ListenOptions.UseHttps

发布于 2025-01-15 21:53:58 字数 1266 浏览 0 评论 0原文

我在 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://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0

我的问题是:如何配置 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_PLACEHOLDER
quot;
        }
      }
    }
  }
}

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));
}

Source - https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0

My question is: how to configure Kestrel to read cert from the .pem file in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖伴 2025-01-22 21:53:58

您可以使用加载它

var pemPath = //read in from configuration
var privateKeyPath = //read in from configuration
var certificate = X509Certificate2.CreateFromPemFile(pemPath, privateKeyPath);

然后您可以在配置 Kestrel 时使用类似的内容配置 Kestrel。

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureKestrel(options =>
        {
            options.ConfigureHttpsDefaults(adapterOptions =>
            {
                adapterOptions.ServerCertificate = certificate
            });
        });
    }

You can just load it in using

var pemPath = //read in from configuration
var privateKeyPath = //read in from configuration
var certificate = X509Certificate2.CreateFromPemFile(pemPath, privateKeyPath);

Then you can configure Kestrel with something like this when you're configuring Kestrel.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.ConfigureKestrel(options =>
        {
            options.ConfigureHttpsDefaults(adapterOptions =>
            {
                adapterOptions.ServerCertificate = certificate
            });
        });
    }
阳光下的泡沫是彩色的 2025-01-22 21:53:58

.NET 7

X509Certificate2 有一个方法 CreateFromPemFile(certPemFilePath, keyPemFilePath)

var builder = WebApplication.CreateBuilder(args);

var pemFile = "cert.pem";
var keyFile = "cert.key";
var port = 5001;  // builder.Configuration.GetSection("https_port").Get<int>();

builder.WebHost
    .ConfigureKestrel(options => {

        options.Listen(IPAddress.Any, port, listenOptions => {

            listenOptions.Protocols = HttpProtocols.Http1AndHttp2;

            var cert = X509Certificate2.CreateFromPemFile(pemFile, keyFile);
            listenOptions.UseHttps(cert);
        });

    });

.NET 7

X509Certificate2 has a method CreateFromPemFile(certPemFilePath, keyPemFilePath):

var builder = WebApplication.CreateBuilder(args);

var pemFile = "cert.pem";
var keyFile = "cert.key";
var port = 5001;  // builder.Configuration.GetSection("https_port").Get<int>();

builder.WebHost
    .ConfigureKestrel(options => {

        options.Listen(IPAddress.Any, port, listenOptions => {

            listenOptions.Protocols = HttpProtocols.Http1AndHttp2;

            var cert = X509Certificate2.CreateFromPemFile(pemFile, keyFile);
            listenOptions.UseHttps(cert);
        });

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文