简体   繁体   English

带有SSL证书的HttpListener挂在GetContext()上

[英]HttpListener with SSL certificate hanging on GetContext()

I've created a windows service which uses a HttpListener to respond to some third-party requests using .NET Framework 4.0. 我创建了一个Windows服务,它使用HttpListener响应使用.NET Framework 4.0的某些第三方请求。 The listener is bound to a https prefix via the following: 侦听器通过以下方式绑定到https前缀:

listener.Prefixes.Add("https://+:" + Properties.Settings.Default.ListeningPort.ToString() + "/");

The service also self registers the https certificate in the computer store via: 该服务还通过以下方式在计算机商店中自行注册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();

Further more, the service also registers the certificate - ip:port binding in Http API using the following: 此外,该服务还使用以下命令在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();

Everything works well, as expected... EXCEPT... (and here comes the EVIL part): After running for some time, an hour or so, listener.GetContext() no longer returns and the clients get dropped with a "connection reset" like error. 一切运作良好,如预期的那样......除了......(这里是EVIL部分):运行一段时间后,一小时左右,listener.GetContext()不再返回,客户端被“连接”删除重置“像错误。

After long and painful investigations I discovered that the error was at the HTTP API level and the failure would occur during the SSL handshake, hence the listener.GetContext() not returning at all. 经过漫长而痛苦的调查后,我发现错误发生在HTTP API级别,并且在SSL握手期间会发生故障,因此listener.GetContext()根本不会返回。 Even more, I discovered that with every failed request, in the System log in Windows Event Log, the following errors would get logged: 更进一步,我发现每次失败的请求,在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.

and

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.

Further digging into the problem I noticed that once, the private key disappeared from the installed certificate. 进一步深入研究这个问题我注意到,一旦私钥从安装的证书中消失了。 That, combined with what I read at this link , made me think different. 这与我在这个链接上读到的内容相结合,让我有所不同。 So I gave this a try: 所以我尝试了一下:

X509Certificate2 certificate = new X509Certificate2(Properties.Settings.Default.HttpsCertPath, "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

Basically, I've put in the X509KeyStorageFlags.Exportable (Remember the error you get when creating a SSL binding if you don't mark the certificate as exportable on IIS?) and X509KeyStorageFlags.PersistKeySet flags. 基本上,我已经放入了X509KeyStorageFlags.Exportable (如果你没有在IIS X509KeyStorageFlags.PersistKeySet证书标记为可导出,请记住创建SSL绑定时遇到的错误吗?)和X509KeyStorageFlags.PersistKeySet标志。 That seems to have done the job. 这似乎完成了这项工作。 The service has been running for almost two days now and still joyfully accepting incoming connections. 该服务现在已经运行了近两天,仍然乐于接受传入的连接。

Hope this will spare someone from the trouble I went through. 希望这能使我免受麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM