https 使用手动 ssl 证书和 kestrel useHttps

Scu*_*iad 3 c# ssl asp.net-web-api kestrel-http-server

我正在尝试使用 https 和 .Net Web API 设置一个简单的 API。

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
                string key = {{private.key}} //is this the password it wants?
                options.Listen(IPAddress.Any, 50790);
                options.Listen(IPAddress.Any, 40354, listenOptions =>
                {
                    listenOptions.UseHttps("certificate.crt", key);
                });
            })
            .Build();

}

//{{private.key}} is the private key in a string.
Run Code Online (Sandbox Code Playgroud)

在启动和连接 http 时使用它可以正常工作,但是一旦我尝试使用 https,我就会收到巨大的错误并且没有响应发送到客户端。

从让我们加密得到一个证书:ca_bundle.crt、certificate.crt 和 private.key。

这是我尝试使用 https 连接时遇到的错误:

失败:Microsoft.AspNetCore.Server.Kestrel[0] 从 IConnectionAdapter 的 OnConnectionAsync 方法未捕获异常。System.NotSupportedException:服务器模式 SSL 必须使用具有关联私钥的证书。在 System.Net.Security.SecureChannel.AcquireServerCredentials(Byte[]& thumbPrint) 在 System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output) 在 System.Net。 System.Net.Security.SslState.StartSendBlob(Byte[] 传入, Int32 计数, AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.ProcessReceivedBlob( Byte[] 缓冲区,Int32 计数,AsyncProtocolRequest asyncRequest) 在 System.Net.Security.SslState.StartReadFrame(Byte[] 缓冲区,Int32 readBytes,

我怎样才能让它工作?

Scu*_*iad 6

问题是证书。

您需要在一个文件中拥有一个带有关联私钥的证书才能使其工作。

所以按照 jdehlin在这里所说的做,并用证书和密钥创建一个 pfx 文件。

当你这样做时,你会被要求为 pfx 文件设置一个密码,这就是你在密码字段中输入的内容,然后你只需链接你的 pfx 文件而不是 crt 文件。