gpt4 book ai didi

c# - 带有卡在 GetContext() 上的 SSL 证书的 HttpListener

转载 作者:可可西里 更新时间:2023-11-01 09:20:02 29 4
gpt4 key购买 nike

我创建了一个 Windows 服务,它使用 HttpListener 来响应一些使用 .NET Framework 4.0 的第三方请求。监听器通过以下方式绑定(bind)到 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 绑定(bind):

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();

一切正常,正如预期的那样……除了……(邪恶的部分来了):运行一段时间后,一个小时左右,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.

进一步深入研究问题后,我注意到有一次私钥从已安装的证书中消失了。结合我在 this link 阅读的内容,让我觉得不一样。所以我试了一下:

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

基本上,我已经放入了 X509KeyStorageFlags.Exportable(还记得如果您没有在 IIS 上将证书标记为可导出的,则在创建 SSL 绑定(bind)时遇到的错误吗?)和 X509KeyStorageFlags.PersistKeySet 标志。这似乎已经完成了工作。该服务现在已经运行了将近两天,并且仍然很高兴地接受传入的连接。

希望这能让别人免于遇到我遇到的麻烦。

关于c# - 带有卡在 GetContext() 上的 SSL 证书的 HttpListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131591/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com