通过HTTP代理使WSHttpBinding工作 [英] Getting WSHttpBinding working through an http proxy

查看:126
本文介绍了通过HTTP代理使WSHttpBinding工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务和一个使用该服务的客户端.他们将WSHttpBinding与MTOM消息编码一起使用.客户端不使用app.config文件,而是在代码中进行所有端点配置. 在正常环境中,这一切都相当不错,但是现在我有一些用户试图从http代理后面连接到该服务.每当他们尝试连接时,都会收到407(需要代理身份验证)警告.

I have a WCF service and a client that uses that service. They use WSHttpBinding with MTOM message encoding. The client doesn't use an app.config file, and does all of the endpoint configuration in code. It all works rather nicely in a normal environment, however I now have some users who are attempting to connect to the service from behind an http proxy. Whenever they try to connect, they get a 407 (Proxy Authentication Required) warning.

我已经设法使用具有连接到代理的专用网络的虚拟机来设置自己的测试环境,因此我可以模拟他们所看到的内容. 我尝试在app.config中设置system.net useDefaultCredentials属性,但这似乎没有任何效果.我检查了正在发送的数据包,它们不包含任何代理身份验证标头.通过代理查看网络流量,他们确实使用了该标头.

I've managed to set up my own testing environment using a virtual machine with a private network that connects to a proxy, so I can simulate what they are seeing. I've tried setting the system.net useDefaultCredentials property in app.config, but that doesn't seem to have any effect. I've examined the packets being sent, and they don't contain any Proxy-Authentication headers. Looking at web traffic through the proxy, they do use that header.

我也尝试过将代理服务器硬编码到客户端中,但这给出了无法连接到服务器"的异常.客户端检查数据包后,会向代理发送3个小数据包,它们都不是http请求,仅此而已.我不确定它在做什么.

I've also tried hard coding the proxy server into the client, but that gives a "cannot connect to server" exception. Examining the packets, the client sends out 3 small ones to the proxy, none of which are an http request, and that's it. I'm not sure what it's doing there.

我什至甚至向客户端添加了一个消息检查器,然后将所需的标头手动注入到请求中,但这并未显示在标头中.

I even went so far as to add a message inspector to the client, and manually inject the required header into the requests, but that's not showing up in the header.

我在这里遇到了麻烦,我真的需要一个解决方案.关于我在这里缺少什么的任何想法或解决方案?

I'm hitting the end of my rope here, and I really need a solution. Any ideas on what I'm missing here, or a solution?

此(带有wsHttpBinding的WCF服务-处理HTTP请求标头)看起来很有希望,但我仍然被困住.

This ( WCF Service with wsHttpBinding - Manipulating HTTP request headers ) seems promising, but I'm still stuck.

这是一部分代码.

            var binding = new WSHttpBinding();
            binding.MaxReceivedMessageSize = 100000000;
            binding.MessageEncoding = WSMessageEncoding.Mtom;
            binding.ReaderQuotas.MaxArrayLength = 100000000;
            binding.OpenTimeout = new TimeSpan(0, 2, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 2, 0);
            binding.SendTimeout = new TimeSpan(0, 2, 0);
            binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

            binding.UseDefaultWebProxy = true;
            // I've also tried setting this to false, and manually specifying the binding.ProxyAddress

            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
            var endpointAddress = new EndpointAddress(new Uri(hostUrl));
            clientProxy_ = new UpdaterServiceProxy(binding, endpointAddress);

            // this behavior was the attempt to manually add the Proxy-Authentication header
            //clientProxy_.Endpoint.Behaviors.Add(new MyEndpointBehavior());

            clientProxy_.ClientCredentials.UserName.UserName = userName;
            clientProxy_.ClientCredentials.UserName.Password = password;
            clientProxy_.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =
                    System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;
            // do stuff...

推荐答案

经过大量实验,我取得了一些进步.事实证明,我可以手动指定代理,它可以正常工作.

After a lot of experimentation I've made some progress. It turns out that I can manually specify the proxy, and it will work.

WebProxy proxy = new WebProxy("http://x.x.x.x:3128", false);
proxy.Credentials = new NetworkCredential("user", "pass");
WebRequest.DefaultWebProxy = proxy;

该代码将在我实例化clientProxy_之前出现.我以前曾尝试过此方法,但由于未指定端口,因此它默默地失败了.我仍然无法获取Windows Internet设置中指定的代理设置.我尝试设置proxy.Credentials = CredentialCache.DefaultNetworkCredentials;,但这似乎也没有效果.

That code appears just before I instantiate my clientProxy_. I had tried this previously, but it silently failed because I hadn't specified the port. I still cannot get it to pick up the proxy settings specified in the Windows Internet Settings. I tried setting proxy.Credentials = CredentialCache.DefaultNetworkCredentials; but that also seemed to have no effect.

我现在也遇到了一个问题,客户端试图使用ChainTrust验证服务的证书,但是对链接证书的请求未使用代理设置.由于这是一个更具体的问题,因此我在此处单独编写了它:证书验证不对链信任使用代理设置

I'm also running into a problem now with the client trying to validate the service's certificate using ChainTrust, but the requests for the chained certificates are not using the proxy settings. Since this is a more specific question, I wrote it up separately here: Certificate validation doesn't use proxy settings for chaintrust

所以我仍然希望获得更多帮助.

So I'm still hoping for more help.

更新: 最后,我只是向应用程序中添加了一个网络配置对话框,以便用户可以指定其代理设置.我一直无法使自动代理设置正常工作.

UPDATE: I ended up just adding a network configuration dialog to my application so the user could specify their proxy settings. I've been unable to get the automatic proxy settings to work properly.

这篇关于通过HTTP代理使WSHttpBinding工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆