5

Is it possible to provide WCF with a custom proxy address and custom credentials?

I've found this answer on stackoverflow: How to set proxy with credentials to generated WCF client?, but I've got a complication, the service I'm authenticating against uses its own authentication, so I've got to use two sets of credentials (one to get through the proxy, and the other to authenticate against the service)

I'm using the technique described in the answers to the other question to provide the service credentials. e.g.

client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;

I can set the address of the proxy using something like this:

(client.Endpoint.Binding as WSHttpBinding).ProxyAddress = ...;

How do I set what is effectively two sets of credentials? (NB: The credentials for the proxy and the actual service are different!) Also note that the proxy details are not necessarily the default system proxy details.

Community
  • 1
  • 1
Gareth
  • 2,424
  • 5
  • 26
  • 44

2 Answers2

14

If you set the WebRequest.DefaultWebProxy property to a new WebProxy with credentials, WCF will use it for all HTTP requests that it makes. (This will affect all HttpWebRequests used by the application unless explicitly overridden).

// get this information from the user / config file / etc.
Uri proxyAddress;
string userName;
string password;

// set this before any web requests or WCF calls
WebRequest.DefaultWebProxy = new WebProxy(proxyAddress)
{
    Credentials = new NetworkCredential(userName, password),
};

My blog post on proxy servers contains further details.

Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108
  • I am consuming a service. The service is authenticated using user name and password. How can we get the proxy address and credentials? – LCJ Nov 28 '12 at 08:56
  • I am getting 502 Bad Gateway http://stackoverflow.com/questions/10807134/azure-service-bus-relay-502-bad-gateway-after-service-restart when I used default proxy as mentioned in http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/5f8d2c3a-164f-411a-b387-91f0be51f190/ – LCJ Nov 28 '12 at 09:05
  • I used this as the basis for our issue where we have a proxy exclusion rule to allow access to our endpoint without credentials but our endpoint has a custom validator so WCF was trying to use the message credentials against the proxy as well. My solution was to replace `Credentials = new NetworkCredential(userName, password)` with `UseDefaultCredentials = true`. This causes WCF to pass no credential to the proxy and pass the username/password in the messgae. Awesome! Thank you! – Shevek Feb 05 '16 at 12:21
2

The client credentials you're setting are fine in order to authenticate to your services.
For proxy authentication you need to use HttpTransportSecurity.ProxyCredentials.

This link might help you out.

http://msdn.microsoft.com/en-us/library/system.servicemodel.httptransportsecurity.proxycredentialtype.aspx

sebagomez
  • 9,501
  • 7
  • 51
  • 89
  • Ye, I saw that. Problem is that I cannot find anywhere to set my username / password for the proxy, thats just an enum of authentication types... – Gareth Oct 09 '08 at 13:24
  • 1
    I haven't tried but for I saw you can set it to take your default proxy credentials, the one you have set in IE – sebagomez Oct 09 '08 at 13:45
  • agreed, and that works. we're wanting to use credentials the user has typed in... (i.e. the user has typed in 2 sets of credentials) – Gareth Oct 09 '08 at 14:06
  • hmm, reading this webpage, it seems that its not possible: http://kennyw.com/indigo/143 – Gareth Oct 09 '08 at 14:11