5

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?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249

2 Answers2

7

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.

Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
  • what about the username and password? –  Jun 20 '09 at 22:54
  • Hi Hans, I just added extra info. Can you try that? I have not used it, but it should work. – Eduardo Scoz Jun 21 '09 at 00:25
  • @Eduardo: he's using WCF. Are you certain that WCF uses the WebRequest.DefaultProxy property? – John Saunders Jun 21 '09 at 00:28
  • 3
    @John - actually - yes! I was surprised myself, too - but check out this link here: http://blogs.msdn.com/stcheng/archive/2008/12/03/wcf-how-to-supply-dedicated-credentials-for-webproxy-authentication.aspx – marc_s Jun 21 '09 at 11:24
  • I actually had a problem with it the other day and that's how I found about it. The option was enabled in the app.config, but no proxy was configured. Somebody then decided to access the web on the performance test server and set up proxy, which made everything stop working.. – Eduardo Scoz Jun 21 '09 at 17:17
  • 1
    Actually you need to set useDefaultWebProxy="false" to use the proxy that you configure – David Jul 13 '10 at 06:29
  • 3
    Thank you, thank you thank you. Is there anything more vile than a network proxy server? Solution above worked perfectly. – Ryan Sorensen Jul 29 '11 at 23:37
  • Great to know it helped, Ryan. – Eduardo Scoz Jul 30 '11 at 17:45
0

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?

Community
  • 1
  • 1
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • I think he wants to be able to set authentication for the proxy server. Also, it might help to show how the code would set them: where did "client" come from in your example, for instance? – John Saunders Jun 20 '09 at 23:47