12

I'm calling a webservice from a WinForms app. Everything works fine when a proxy server isn't in use, however when a proxy is being used, the app crashes as instead of the XML response it's expecting for the SOAP request, it gets an HTML error page saying "Authentication Required".

It seems you can set the proxy manually like this:

WebClient client = new WebClient();
WebProxy wp = new WebProxy("proxy server url here");
client.Proxy = wp;

...but to some extent, it seems to be seeing the proxy server anyway WITHOUT doing the above, as the error generated is actually coming from the proxy server. It just doesn't seem to be picking up the Windows Authentication login credentials from the user's computer. How can I force it to do this?

On my own machine if I simulate this using Fiddler (and enabling the "Require Proxy Authentication" option), I get a dialog pop up asking for the login credentials, but this doesn't seem to happen on my client's machines (who use a real hardware proxy - McAfee Web Gateway).

How can I handle this? Do I need to provide a dialog for users to configure the server manually or is there a setting to tell WebClient to use the Windows default proxy and the user's own login credentials?

Update

Seems like you can pick up the proxy server using the code below, but that doesn't cause the authentication dialog to appear in all situations (works on some PCs but not on others):

IWebProxy defaultProxy = WebRequest.DefaultWebProxy;
if (defaultProxy != null)
{
    defaultProxy.Credentials = CredentialCache.DefaultCredentials;
    client.Proxy = defaultProxy;
}

If the code above is correct, I don't understand why some users would not be prompted for their credentials. Do I have to put in my own code to collect the user credentials and supply them to the WebRequest object?

NickG
  • 9,315
  • 16
  • 75
  • 115
  • Have you looked at the [WindowsIdentity.Impersonate Method](http://msdn.microsoft.com/en-us/library/w070t6ka%28v=vs.100%29.aspx) ? – Joshua Drake Jun 06 '13 at 16:11
  • 1
    Not sure that's relevant as my app is already running as the local user I think? – NickG Jun 06 '13 at 16:23
  • Doh, my bad... I've been dealing with SQL CLRSprocs too much lately... "When all you [use] is a hammer..." – Joshua Drake Jun 06 '13 at 16:31
  • WebClient will, as you've noted, "set by the system using configuration files and the Internet Explorer Local Area Network settings" - [WebClient.Proxy Property](http://msdn.microsoft.com/en-us/library/system.net.webclient.proxy%28v=vs.100%29.aspx). If the client is not being prompted for credentials, either they have IE setup not to ask for them, which can be configured by the site classification in IE, or their proxy does not require them, possibly because the site the app is hitting is in their domain already? – Joshua Drake Jun 06 '13 at 16:36
  • Possibly related: http://stackoverflow.com/questions/844467/c-sharp-auto-detect-proxy-settings – Joshua Drake Jun 06 '13 at 16:59
  • What is the exact error message? What kind of authentication does the proxy server use? If it's NTLM there are a few tricks I can provide. – Brad Bruce Jun 20 '13 at 17:56
  • @GONeale No I will not accept his answer as it doesn't work for me and has no effect at all on solving my problem. – NickG Aug 21 '13 at 08:27
  • Ok well that's a good reason not to accept it ;) That's strange then? Why doesn't it work for you? As soon as I added it, it started inheriting IE proxy settings. – GONeale Aug 27 '13 at 05:13
  • Because it relies on the user credentials being in the cache, which they never were on any system I tested on - even if I'd just logged in. I don't know when the credentials are in the cache, but I just found they weren't so this solution didn't help me. – NickG Aug 27 '13 at 13:56
  • WebProxy.GetDefaultProxy is deprecated. – Robert Bratton May 01 '14 at 15:02

3 Answers3

19

Try adding

  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>

to your app.config file

yclkvnc
  • 913
  • 8
  • 15
5
using (WebClient webClient = new WebClient())
{

    webClient.UseDefaultCredentials = true;
    webClient.Proxy = WebRequest.GetSystemWebProxy();
}

this should work

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
umarfarukhT
  • 69
  • 2
  • 6
0

First try to use this:

WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
if (proxy.Address.AbsoluteUri != string.Empty)
{
    Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
    client.Proxy = proxy;
}

if this does not work try with:

WebProxy proxy = WebProxy.GetDefaultProxy()
client.Proxy = proxy;
Cilenco
  • 6,951
  • 17
  • 72
  • 152