【问题标题】:Is it possible to specify 2 different SSL certificates for a single instance of Kestrel server?是否可以为 Kestrel 服务器的单个实例指定 2 个不同的 SSL 证书?
【发布时间】:2017-05-06 18:49:03
【问题描述】:

我知道“KestrelServerOptions”类中有一个方法“UseHttps”接受单个证书,但是否可以指定多个 SSL 证书以便能够在一个域中使用多个域和多个证书Kestrel 的单个实例?

【问题讨论】:

  • 不确定你能做到这一点。如果这是一个要求,您最好在 Kestrel 应用程序前面运行 IIS 或 Nginx 作为代理服务器,并在那里处理您的 SSL。
  • 你能解决什么问题吗?我真的很想这样做!
  • @Ichirichi 不,我没有解决它

标签: ssl https kestrel-http-server


【解决方案1】:

可以通过在调用 UseHttps() 时设置 ServerCertificateSelector 属性来实现。 选择器是您自己实现的回调,它提供了一个字符串参数,该参数指示客户端用于与您的服务器通信的 DNS 主机是什么。您可以处理这个参数并返回相关的证书。

取自Microsoft docs

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

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 2014-12-05
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    相关资源
    最近更新 更多