我正在尝试使用 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/