![](/img/trans.png)
[英]Mono HTTPListener throws exception when using self signed SSL certificate
[英]HttpListener with SSL certificate hanging on GetContext()
我创建了一个Windows服务,它使用HttpListener响应使用.NET Framework 4.0的某些第三方请求。 侦听器通过以下方式绑定到https前缀:
listener.Prefixes.Add("https://+:" + Properties.Settings.Default.ListeningPort.ToString() + "/");
该服务还通过以下方式在计算机商店中自行注册https证书:
X509Certificate2 certificate = new X509Certificate2(Properties.Settings.Default.HttpsCertPath, "", X509KeyStorageFlags.MachineKeySet);
X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();
此外,该服务还使用以下命令在Http API中注册证书-ip:port binding:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "netsh";
psi.Arguments = "http add sslcert ipport=0.0.0.0:" + Properties.Settings.Default.ListeningPort.ToString() + " certhash=" + certificate.Thumbprint + " appid=" + appId;
Process proc = Process.Start(psi);
proc.WaitForExit();
psi.Arguments = "http add sslcert ipport=[::]:" + Properties.Settings.Default.ListeningPort.ToString() + " certhash=" + certificate.Thumbprint + " appid=" + appId;
Process proc = Process.Start(psi);
proc.WaitForExit();
一切运作良好,如预期的那样......除了......(这里是EVIL部分):运行一段时间后,一小时左右,listener.GetContext()不再返回,客户端被“连接”删除重置“像错误。
经过漫长而痛苦的调查后,我发现错误发生在HTTP API级别,并且在SSL握手期间会发生故障,因此listener.GetContext()根本不会返回。 更进一步,我发现每次失败的请求,在Windows事件日志的系统日志中,都会记录以下错误:
Source Schannel: A fatal error occurred when attempting to access the SSL server credential private key. The error code returned from the cryptographic module is 0x8009030D. The internal error state is 10001.
和
Source HttpEvent: An error occurred while using SSL configuration for endpoint 0.0.0.0:9799. The error status code is contained within the returned data.
进一步深入研究这个问题我注意到,一旦私钥从安装的证书中消失了。 这与我在这个链接上读到的内容相结合,让我有所不同。 所以我尝试了一下:
X509Certificate2 certificate = new X509Certificate2(Properties.Settings.Default.HttpsCertPath, "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
基本上,我已经放入了X509KeyStorageFlags.Exportable
(如果你没有在IIS X509KeyStorageFlags.PersistKeySet
证书标记为可导出,请记住创建SSL绑定时遇到的错误吗?)和X509KeyStorageFlags.PersistKeySet
标志。 这似乎完成了这项工作。 该服务现在已经运行了近两天,仍然乐于接受传入的连接。
希望这能使我免受麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.