Proxy server authentication for WCF service

2019-03-30 04:01发布

I need to consume a WCF service but I'm behind a proxy server and this proxy server requires a username and password.

I can't find a way to set it, if it was a Web Service, I could just do something like

ws.Proxy = myProxyServer;

How can I do this with a WCF service?

标签: wcf proxy
2条回答
做个烂人
2楼-- · 2019-03-30 04:05

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

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-30 04:15

Note replaced previous answer based on comment

There was actually another stackoverflow answer that covered setting credentials on a proxy.

Is it possible to specify proxy credentials in your web.config?

查看更多
登录 后发表回答