12

My use case is this, I want to call out to a webservice and if I am behind a proxy server that requires authentication I want to just use the default credentials...

  WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;

Otherwise I'll just simply make the call, It would be very nice to determine if the auth is required up front, rather than handle the exception after I attempt to make the call.

Ideas?

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
  • Just out of curiosity, if you're only using the default credentials, what would be the downside to applying these credentials all the time, even when they're not needed? – BFree Jan 29 '09 at 03:53
  • actually I haven't tried with default credentials, only with specific credentials, same class though (DefaultWebProxy) and that threw an exception, but good call, I'll try it with the default creds when I get home from work (I expect it to throw the same exception though) – Tim Jarvis Jan 29 '09 at 04:15

5 Answers5

14

It was only after I had first deployed my app that I realised some users were behind firewalls... off to work to test it. Rather than do a test for a '407 authentication required' I just do the same Proxy setup whether it might be needed or not...

System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri.AbsoluteUri);
//HACK: add proxy
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
req.Proxy = proxy;
req.PreAuthenticate = true;
//HACK: end add proxy
req.AllowAutoRedirect = true;
req.MaximumAutomaticRedirections = 3;
req.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; DeepZoomPublisher.com)";
req.KeepAlive = true;
req.Timeout = 3 * 1000; // 3 seconds

I'm not sure what the relative advantages/disadvantages are (try{}catch{} without proxy first, versus just using the above), but this code now seems to work for me both at work (authenticating proxy) and at home (none).

Conceptdev
  • 3,261
  • 1
  • 21
  • 23
  • I don't think the PreAuthenticate flag here is specifically required, this only causes the request to authenticate immediately after the first request to the web server if credentials are set – Willem D'Haeseleer Apr 29 '13 at 06:34
4

System.Net.WebProxy has a property called UseDefaultCredentials that may be what you want (but I have to admit a bit of ignorance here). The link to the relevant documentation is here.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
1

Actually, seems that this isn't an issue after all, Previously I was setting the auth like so...

  WebProxy proxy = new WebProxy(@"http://<myProxyAddress>:8080");
  proxy.Credentials = new NetworkCredential(<myUSerName>, <myPassword>, <myDomain>);
  WebRequest.DefaultWebProxy = proxy;

This would be fine for when I was behind the proxy but throw an error when there was no proxy, so of course I expected the same error above as I was still just setting the same credentials, but you know what they say about assuming things....in fact there is no error at all with setting the default creds, all is sweet.

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
0

If you want to check for the Proxy settings in IE, you could also peek into the registry: check the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings branch of the registry tree - lots of options and settings there. Most notably: ProxyEnable (a DWORD, 0 = no proxy, 1 = proxy enabled).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
-1

It looks like if you leave the Proxy stuff alone, .NET should just use the IE proxy settings, which seems like the most "correct" way of dealing with proxies...

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328