Reputation: 33
I've spent sometime search through articles on this site to find exactly what I am trying to accomplish, but so far no luck. I am performing a web request in my client side application, and I want to make sure that it works through a proxy. I set up Fiddler2 to act as the proxy server for testing, and I am forcing it to require authentication.
I have essentially figured out how to get the proxy to use:
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
webService.Proxy = proxy
I have also tried to get the proper credentials to the proxy by
webService.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
and also by adding this to the app.config under the configuration section:
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
Both cases I get the same web exception:
The request failed with HTTP status 407: Proxy Auth Required.
Fiddler2's proxy has a username of 1 and a password of 1, and if I give these as the credentials to the request programmatically, the request will authenticate successfully. However, is there a reason that it is failing to grab the credentials from IE just like it is grabbing the proxy information?
Is Fiddler's proxy an exception to how this works too? Would a normal proxy that uses Windows Authentication be fine with the ways I tried to obtain the credentials, and is there any way I can easily test that?
Upvotes: 3
Views: 4143
Reputation: 57085
Most, but not all proxies that require authentication use the current user's Windows login credentials. As a consequence, using DefaultCredentials is typically the right thing to do.
Having said that, it's possible for a proxy to demand custom credentials (Fiddler wants "1:1", for instance) and when that happens there's no way for your code to really know what those credentials are. You may have some luck asking CredMan to see if there's a stored credential for the target proxy realm, but this often will not work (assuming it ever does). You can't generally "ask IE" for the credentials because as a general rule, it doesn't persistently store the credentials; instead, it prompts the user for them on the first request of the session and keeps them in memory for the lifetime of that session.
If you expect your software to be run in an environment with such a proxy, you should trap the HTTP/407 response and prompt the user for their proxy credentials, which you can manually add to the request upon a retry.
See my IEInternals blog post for discussion of this topic.
Upvotes: 2