我正在try 使用证书存储中的证书为kestrel配置HTTPS.以下是我到目前为止所取得的成就:

appsettings.json

{
  "SSLCertificate": {
    "Serial": "serialNumberFromCertificateStore"
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "https://*:8090",
        "Protocols": "Http1"
      },
      "gRPC": {
        "Url": "https://*:8091",
        "Protocols": "Http2"
      }
    }
  }
}

Program.cs

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});

builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
    var kestrelSection = context.Configuration.GetSection("Kestrel");
    var certSerial = context.Configuration.GetSection("SSLCertificate").GetValue<string>("Serial");

    if (!string.IsNullOrEmpty(certSerial))
    {
        // Retrieve the certificate from the Windows certificate store
        using var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);
        var certificate = store.Certificates.Where(f => f.SerialNumber.ToUpper().Equals(certSerial.ToUpper())).FirstOrDefault();

        if (certificate != null)
        {
            serverOptions.Configure(kestrelSection);
            // Configure HTTPS endpoint with the retrieved certificate
            serverOptions.ListenAnyIP(8090, listenOptions =>
            {
                listenOptions.UseHttps(certificate);
            });
        }
    }
});

我从appsettings.json中读取证书序列号,并在检索证书后将其应用于HTTPS方法中.问题是它try 开始监听端口8090两次.如果我改变了端口,它会监听该端口并识别证书,但不会做出任何响应.

我在这里应该做什么?

推荐答案

您可以而且可能应该通过appsettings.json文件自动管理大部分内容.在the kestrel docs中有详细的说明.

我推荐的最重要的一点是,在设置中使用序列号为not.该will在某个点到期,并且用于证书续订will not的大多数自动化过程自动改变该设置.

取而代之的是引用证书的"主题",它不会从一个证书续订到下一个证书续订.

大概是这样的:

"Kestrel": {
    "Endpoints": {
        "Http": {
            "Url" : "https://*:8090",
            "Protocols": "Http1"
        },
        "gRPC": {
            "Url" : "https://*:8091",
            "Protocols": "Http2"
        }
    },
    "Certificates": {
        "Default": {
            "Subject": "MyWebServer",
            "Store": "My",
            "Location": "LocalMachine"
        }
    }
}

这不需要编写任何额外的代码就可以从存储中获取证书,因为所有值都已在配置中设置.如果找不到合适的证书,它将抛出异常,如果您使用的是自签名证书(如appsettings.Development.json中的默认本地主机ASP.NET dev证书),则可能需要在Certificate设置的基础上增加"AllowInvalid": true.

Csharp相关问答推荐

自定义JsonEditor,用于将SON序列化为抽象类

下拉图片点击MudBlazor

如何使用C#中的图形API更新用户配置文件图像

在Linq中调用需要limit和offset的方法''''

数组被内部函数租用时,如何将数组返回给ArrayPool?

C#中使用BouncyCastle计算CMac

将现有字段映射到EFCore中的复杂类型

在.NET 7.0 API控制器项目中通过继承和显式调用基类使用依赖项注入

是否有必要在ASP.NET Core中注册可传递依赖项?

当试图限制EF Select 的列时,如何避免重复代码?

Blazor Fluent UI DialogService,<;FluentDialogProvider/>;错误

为什么此名称不再被识别?名称不存在于当前上下文中?

使用switch 类型模式时出现奇怪的编译器行为

在';、';附近有错误的语法.必须声明标量变量";@Checkin";.';

在同一个捕获中可以有多种类型的异常吗?

从HTML元素获取 colored颜色

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

在ObservableCollection上使用[NotifyPropertyChangedFor()]源代码生成器不会更新UI

根据优先级整理合同列表

使用';UnityEngineering.Random.Range()';的IF语句仅适用于极高的最大值