Kestrel ssl JSON 配置中的证书问题

Certificate issue in Kestrel ssl JSON configuration

参考 Kestrel documentation 是否可以使用 appsettings.json 文件配置 https:

  "HttpsInlineCertStore": {
    "Url": "https://+:5002",
    "Certificate": {
      "Subject": "<coma separated multi-line subject name>",
      "Store": "Root",
      "Location": "LocalMachine"
  }

此证书肯定存在,下一个代码 returns 找到它:

        using (var certStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine))
        {
            certStore.Open(OpenFlags.ReadOnly);
            var certificates = certStore.Certificates.Find(
                X509FindType.FindBySubjectDistinguishedName, "<coma separated multi-line subject name>", true);

            return certificates .Count > 0 ? certificates [0] : null;;
        }

同时,如果通过 X509FindType.FindBySubjectName 搜索证书,它什么也找不到,我相信这就是问题所在,即使 microsoft 说 FindBySubjectDistinguishedName 是更具体的搜索。

我终于解决了这个问题: 类似于 "CN=name, C=UK, ..." 但如果你想 FindBySubjectName 你必须从搜索字符串中删除 "CN=" 并只留下名称所以它看起来不像 "CN=name" 而像 "name ".