【问题标题】:Load an SSL certificate from appsettings.json从 appsettings.json 加载 SSL 证书
【发布时间】:2020-01-13 11:31:01
【问题描述】:

我构建了一个 .NET core 2.2 应用程序,并将其加载到树莓派上。我正在使用 Kestrel 托管 Web 服务。在 pi 上,我创建了一个自分配的 certificate.pfx。如果我用证书名称和密码将 .UseHttps 硬编码到应用程序中,浏览器可以找到它。

但是,如果我将其从代码中注释掉并改用 appsettings.json 文件(我希望它在 appsettings.json 中,以便客户端可以上传自己的证书),重定向到 Https 将起作用,但是证书未加载,页面无法连接。

这是我用来配置我的 appsettings.json 文件的文档: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2

证书位于应用程序文件夹中。

目前我已将此代码从应用程序中注释掉,但在未注释代码时它确实有效。我希望通过 appsettings.json 文件来设置这些相同的设置。

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureKestrel((context, options) =>
                {                   
                    /*options.Listen(IPAddress.Any, 80);*/         // http:*:80                 
                    //options.Listen(IPAddress.Any, 5001, listenOptions =>
                    //{
                    //  listenOptions.UseHttps("certificate.pfx", "password");
                    //});
                });

这里是 appsettings.json 文件:

{
  "https_port": 5001,
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "AllowInvalid": true,
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://*:80"
      }
    },
    "Https": {
      "Url": "https://*:5001",
      "Certificate": {
        "Path": "certificate.pfx",
        "Password": "password"
      }
    }
  }
}

【问题讨论】:

  • 来自文档Set AllowInvalid to true to permit the use of invalid certificates (for example, self-signed certificates).,您可以尝试使用证书存储字段指定证书吗?
  • 或者你可以使用program.cs中的配置,参考github.com/aspnet/KestrelHttpServer/issues/…

标签: linux ssl asp.net-core raspberry-pi3 kestrel


【解决方案1】:

这篇文章最终得到了我需要的解决方案:

https://www.humankode.com/asp-net-core/develop-locally-with-https-self-signed-certificates-and-asp-net-core

更准确地说,它是标题为“在 ASP.NET Core 2.0 中配置 HTTPS”的文章的一部分

我没有使用 appsettings.json,而是创建了一个名为 certificate.json 的新 json 文件。

然后我将证书名称和密码从 certificate.json 拉入到 program.cs 代码中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 2016-08-02
    • 2014-03-19
    • 1970-01-01
    • 2015-11-15
    • 2015-07-02
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多