views:

3176

answers:

3

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

I've found this answer on stackoverflow: http://stackoverflow.com/questions/105499/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.

+1  A: 

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

sebastian
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
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
sebastian
typed too fast :)I have no tried it, but for what I saw...
sebastian
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
hmm, reading this webpage, it seems that its not possible:http://kennyw.com/indigo/143
Gareth
+1  A: 

From the comments to my answer I guess it is not possible to have different credentials for service authentication and http proxy.

that suck! :(

sebastian
ye. oh well. We're going with a "Use the default system proxy" or "Use no proxy" option...Thanks for your help!
Gareth
+1  A: 

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