c# - https 使用带有 kestrel useHttps 的手动 ssl 证书

标签 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.

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

从 lets encrypt 获得证书:ca_bundle.crt、certificate.crt 和 private.key。

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

fail: Microsoft.AspNetCore.Server.Kestrel[0] Uncaught exception from the OnConnectionAsync method of an IConnectionAdapter. System.NotSupportedException: The server mode SSL must use a certificate with the associated private key. at System.Net.Security.SecureChannel.AcquireServerCredentials(Byte[]& thumbPrint) at System.Net.Security.SecureChannel.GenerateToken(Byte[] input, Int32 offset, Int32 count, Byte[]& output) at System.Net.Security.SecureChannel.NextMessage(Byte[] incoming, Int32 offset, Int32 count) at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)

我怎样才能让它工作?

最佳答案

问题是证书。

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

所以按照 jdehlin 所说的做 here并创建了一个包含证书和 key 的 pfx 文件。

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

关于c# - https 使用带有 kestrel useHttps 的手动 ssl 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48722386/

相关文章:

c# - 我需要使用 Web API 和 JSON 通过互联网传输 100 万行

c# - 使用无类型模型将表单发布到 WebAPI OData Controller

c# - ShutdownBlockReasonCreate - 创建在注销/关机期间显示的多个原因

c# - 如何在 C# 上使用 G Suite Admin SDK 获取组织(orgUnit)中的所有用户?

ssl - 服务器和客户端之间的 key 交换

apache http 重定向到 https 多个重写条件

c# - SOCKET 聊天 C# 与私有(private)消息

c# - 上下文切换死锁

python - 如何解决错误 :SDL_OpenAudio (2 channels, 44​​100 Hz):没有这样的音频设备

c# - Firefox 没有 Access-Control-Allow-Origin,但适用于 Chrome 和 Safari