[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("https://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).
=========================
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="False"
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ServiceSoap"
........................
useDefaultWebProxy="false"
bypassProxyOnLocal="false"
proxyAddress="https://localhost:8888/">
=======================
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:
https://social.msdn.microsoft.com/Forums/en-US/wcf/thread/f2074c0d-4922-402b-8bcb-e653ab04644b/
https://www.groupsrv.com/dotnet/post-908381.html
Hope this helps.
Comments
Anonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=steven-cheng-s-msdn-notes-wcf-how-to-supply-dedicated-credentialsAnonymous
July 10, 2009
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? Thanks, JenniferAnonymous
July 22, 2009
Hi Steven, Thank you very much for this information.I spent the whole morning searching for this.Anonymous
November 30, 2009
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.Anonymous
March 11, 2012
Hi steven,Thank u soooo much.it's really healpful for me.Anonymous
March 12, 2013
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; }