WCF 服务的代理服务器身份验证
我需要使用 WCF 服务,但我在代理服务器后面,而该代理服务器需要用户名和密码.
I need to consume a WCF service but I'm behind a proxy server and this proxy server requires a username and password.
我找不到设置它的方法,如果它是 Web 服务,我可以做类似的事情
I can't find a way to set it, if it was a Web Service, I could just do something like
ws.Proxy = myProxyServer;
如何使用 WCF 服务执行此操作?
How can I do this with a WCF service?
在 WCF 绑定配置中,使用 useDefaultWebProxy 属性使 WCF 使用 windows 默认代理(可以从 IE 网络配置中设置):
In the WCF binding config, use the useDefaultWebProxy property to make WCF use the windows default proxy (which can be set from IE network config):
<bindings>
<basicHttpBinding>
<binding name="ESBWSSL" ...everything... useDefaultWebProxy="true">
然后在代码中,在使用连接之前,这样做:
Then in the code, before you use the connection, do this:
WebProxy wproxy = new WebProxy("new proxy",true);
wproxy.Credentials = new NetworkCredential("user", "pass");
并使用您的 webrequest 对象,在您执行调用之前:
and with your webrequest object, before you execute the call:
WebRequest.DefaultWebProxy = wproxy;
我还没有测试过代码,但我相信这应该可行.
I have not tested the code, but I believe this should work.