Search code examples
c#wcfproxyhttp-proxy

WCF basicHttpBinding and HTTP proxy


I'm trying to get my generated WCF ServiceClient to send its requests to the WCF service through a HTTP (not HTTPS) proxy with username/password authentication, however I just can't get it work. My WCF service uses basicHttpBinding so I tried to configure my ServiceClient instance like so:

var svc = new ServiceClient();
var b = svc.Endpoint.Binding as BasicHttpBinding;

b.ProxyAddress = new Uri(proxyAddress);
b.UseDefaultWebProxy = false;
b.Security.Mode = BasicHttpSecurityMode.Transport;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
b.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

svc.ClientCredentials.UserName.UserName = proxyUsername;
svc.ClientCredentials.UserName.Password = proxyPassword;

This however results in a System.ArgumentException saying:

The provided URI scheme http is invalid. expected https

When I set the b.Security.Mode to BasicHttpSecurityMode.None though, it seems the HTTP proxy settings are ignored by WCF altogether!

The second solution I tried was to set the DefaultWebProxy property of WebRequest and set the UseDefaultWebProxy property to true such as so:

var webProxy = new WebProxy(proxyAddress, true);
webProxy.Credentials = new NetworkCredential(proxyUsername, proxyPassword);
WebRequest.DefaultWebProxy = webProxy;

var svc = new ServiceClient();

var b = svc.Endpoint.Binding as BasicHttpBinding;
b.UseDefaultWebProxy = true;

However this also doesn't work and ServiceClient doesn't send it's requests through the HTTP proxy.

I'm out of ideas here so please let me know what I am doing wrong, thank you!


Solution

  • Set the security mode to BasicHttpSecurityMode.TransportCredentialOnly. This allows for passing plain-text authentication details over HTTP.