通过 HttpWebRequest 发送客户端证书,间歇性工作

发布于 2024-09-26 10:08:47 字数 1695 浏览 5 评论 0原文

我创建了一个 Web 应用程序,该应用程序调用需要客户端证书进行身份验证的 Web 服务。以下是我如何构建请求的片段:

            // Grab Certificate
            X509Certificate2 cert2 = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + GiftCardConfig.A2A_CertificateLocation, GiftCardConfig.A2A_CertificatePassword, X509KeyStorageFlags.MachineKeySet);

            // First Call Status Account
            StringBuilder urlStatus = new StringBuilder(GiftCardConfig.A2A_URL + "webservice.asp?");
            urlStatus.Append("userid=" + GiftCardConfig.A2A_UserID);
            urlStatus.Append("&pwd=" + GiftCardConfig.A2A_Password);
            urlStatus.Append("&sourceid=" + GiftCardConfig.A2A_SourceID);
            urlStatus.Append("&cardnum=" + cardNumber);
            urlStatus.Append("&purseno=" + GiftCardConfig.A2A_PurseID);

            urlStatus.Append("&status=ACTIVATE");

            // Build HTTP Request
            HttpWebRequest wrStatus = (HttpWebRequest)WebRequest.Create(urlStatus.ToString());
            wrStatus.KeepAlive = true;
            wrStatus.Method = "GET";
            wrStatus.Accept = "text/xml";
            wrStatus.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)";
            wrStatus.ClientCertificates.Clear();
            wrStatus.ClientCertificates.Add(cert2);

这一切都有效,但只是间歇性地起作用。大约每 24 小时,托管 Web 服务的服务器就会返回 403:Forbidden 错误。修复此问题的唯一方法是对运行 Web 应用程序的服务器执行 iisreset。我们对这个问题完全感到困惑,并想知道这个问题是否与 Web 应用程序或其托管服务器的配置有关。

任何可能的解决方案或意见将不胜感激!

I have created a web application that calls a web service that requires a Client Certificate for authentication. Here is a snippet of how I am building the request:

            // Grab Certificate
            X509Certificate2 cert2 = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + GiftCardConfig.A2A_CertificateLocation, GiftCardConfig.A2A_CertificatePassword, X509KeyStorageFlags.MachineKeySet);

            // First Call Status Account
            StringBuilder urlStatus = new StringBuilder(GiftCardConfig.A2A_URL + "webservice.asp?");
            urlStatus.Append("userid=" + GiftCardConfig.A2A_UserID);
            urlStatus.Append("&pwd=" + GiftCardConfig.A2A_Password);
            urlStatus.Append("&sourceid=" + GiftCardConfig.A2A_SourceID);
            urlStatus.Append("&cardnum=" + cardNumber);
            urlStatus.Append("&purseno=" + GiftCardConfig.A2A_PurseID);

            urlStatus.Append("&status=ACTIVATE");

            // Build HTTP Request
            HttpWebRequest wrStatus = (HttpWebRequest)WebRequest.Create(urlStatus.ToString());
            wrStatus.KeepAlive = true;
            wrStatus.Method = "GET";
            wrStatus.Accept = "text/xml";
            wrStatus.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)";
            wrStatus.ClientCertificates.Clear();
            wrStatus.ClientCertificates.Add(cert2);

This all works, but only intermittenly. About every 24 hours the Server hosting the web service returns a 403:Forbidden error. The only way to fix it is to do a iisreset of the server running the web application. We are completely stumped about this issue and would like to know if this issue has something to do with the web application or the configuration of the server it is being hosted on.

Any possible solutions or comments are greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2024-10-03 10:08:47

您可以尝试将 keeplive 设置为 false,尽管这可能会影响性能。

You could try setting keep alive to false, though there may be a performance hit with this.

榆西 2024-10-03 10:08:47

只是万一其他人遇到这个问题。我们从未确定过原因,但是在服务器上重新安装 .NET 框架似乎“解决”了该问题。尽管并不理想,但这是 5 年来第一次发生这种情况,因此对我们目前的解决方案感到满意。

Just on the off chance anyone else comes across this. We never did identify the cause, however re-installing the .NET framework on the server seemed to 'resolve' the issue. Although not ideal, this is the first time it happened in 5 years, therefore happy enough with our resolution for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文