.net 在kestrel服务器中使用证书配置HTTPS的内置方式

sqxo8psd  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(127)

新手在这里在ASP.NET核心应用程序中是否有内置的方法来配置HTTPS和证书?这是我尝试的。首先,我在settings.json中设置url和证书详细信息

{
  "Kestrel": {
    "Endpoints": {
      "Https": {
        "Url": "https://localhost:5001"
        }
      },
    "Certificates": {
      "Default": {
          "Subject": "<subject; required>",
          "Store": "<certificate store; required>",
          "Location": "<location; defaults to CurrentUser>",
          "AllowInvalid": "<true or false; defaults to false>"
                }
                  }
              }
}

字符串
然后使用配置管理器加载配置

builder.Configuration
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("settings.json", false, true)
    .AddEnvironmentVariables();


当我尝试使用postman触发API时,我得到“错误:在建立安全TLS连接之前,客户端网络套接字已断开连接”。控制器在settings.json文件中没有Certificates.Defaults部分时工作正常。
我错过了什么?

q3qa4bjr

q3qa4bjr1#

这是appsettings.Production.json的一部分,它工作得很好(Net 6/7):

"Kestrel": {
    "EndPoints": {
      "Http": {
        "Url": " ... "
      },
      "Https": {
        "Url": " ... ",
        "Certificate": {
          "Path": " (your_cert_file.pfx) ",
          "Password": " (password) "
        },
      }
    }
  },

字符串
证书文件路径是相对于工作文件夹的。

相关问题