[WCF]How to supply dedicated credentials for webproxy authentication in WCF client

[WCF]How to supply dedicated credentials for webproxy authentication in WCF client


I’ve found several issue request on how to supply credentials for webproxy(intermediate proxy server) authentication in client application which consumes WCF service. When performing some remote network accessing such as WebRequest call, Webservice call or WCF call, it’s common that the client will need to provide some credentials for the service application’s authentication. However, it is also possible that client need to supply credentials for some intermediate proxy server’s authentication. In .NET framework, the WebRequest and WebService(client proxy) provide “Proxy” property(of System.Net.WebProxy type) that can help supply such credentials. E.g.


HttpWebRequest req = (HttpWebRequest)WebRequest.Create(“http://www.msn.com”);

req.Proxy.Credentials = CredentialCache.DefaultCredentials;

WebResponse rsp = req.GetResponse();


However, the WCF client proxy(generated via “Add ServiceReference” or svcutil.exe) doesn’t expose such a property. There does exists some configuration entry like below which help specify some network proxy info, but none of them help us explicitly specify a credentials (username/password pair).




<defaultProxy useDefaultCredentials=”true”>

<proxy usesystemdefault=”False”






      <binding name=”ServiceSoap”







After some research, I found that the built-in WCF authentication model will rely on the same set of client-side credentials(for service authentication).


ChannelFactory<IService> communication = new ChannelFactory<IService>(binding, endpoint);

//can only set 1 set of Username/Password credential (is used for both the web proxy and the service authentication)

communication.Credentials.UserName.UserName = “user”;

communication.Credentials.UserName.Password = “pass”;


Then, what if we need to supply different credentials for service and web proxy authentication respectively? Fortunately, there is still a means for us to supply dedicated credentials for web proxy in WCF client code. That’s the “WebRequest.DefaultWebProxy” property. The WCF runtime will acquire proxy setting from WebRequest.DefaultWebProxy property if the <system.net> and WCF <binding> remains useDefaultproxy as “true”.


Thus, our final code will simply work as below:


//first customize the default proxy

WebProxy wproxy = new WebProxy(“new proxy”,true);

wproxy.Credentials = CredentialCache.DefaultNetworkCredentials;

//or you can construct your own credentials via System.Net.NetworkCredential


WebRequest.DefaultWebProxy = wproxy;


//do WCF calls….

WCFSVC.ServiceClient client = new ConsoleClient.WCFSVC.ServiceClient();



Here are some other threads discussing on this issue:








Hope this helps.






Comments (6)

  1. Jennifer says:

    Hi Steve,

    After the configuration it returned an new error:

    : 417 “Expectation Failed.”

    I added System.Net.ServicePointManager.Expect100Continue = false;

    and it worked. ref:http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c

    Can you let me know about this?



  2. Igor says:

    Hi Steven,

    Thank you very much for this information.I spent the whole morning searching for this.

  3. pjb says:

    I have a WPF browser app (partial trust) that calls a WCF service that runs on the web server. All works fine when I am not behind a proxy server. If I am, I get the message: "(407) Proxy Authentication Required". If I try to alter the <defaultProxy> settings I get the message "Insufficient permissions for setting the configuration section ‘defaultProxy’". What should I do? I just want to use the same credentials for normal web access and WCF access.

  4. sweety says:

    Hi steven,Thank u soooo much.it's really healpful for me.

  5. mlh says:

    Same as Sweety, many thanks!

    In my scenario, I have an ASP.Net app and couple "web references", some ASMX and some WCF. All the sudden the infra team added a proxy requiring authentication.

    Below is what i did to make it work for both asmx and wcf clients.

    In Global.asax.cs

    protected void Application_Start(Object sender, EventArgs e)


     WebProxy wproxy = new WebProxy("myproxyhost", <myproxyPort>) { Credentials = CredentialCache.DefaultCredentials };

     WebRequest.DefaultWebProxy = wproxy;