How to use IE proxy settings and credentials in .Net application

How to use saved proxy settings and default credentials for HttpWebRequests? Proxy settings are available and used, but not credentials:

IWebProxy proxy = WebRequest.GetSystemWebProxy(); proxy.Credentials = CredentialCache.DefaultNetworkCredentials; WebRequest.DefaultWebProxy = proxy; 

Are there any credential permissions?

It works with passing credentials through an instance of NetworkCredential:

 proxy.Credentials = new NetworkCredential("username", "password"); 

But I would like to use saved from the operating system / IE.

Edit:

I use a third-party lib that creates and calls HttpWebRequests, which must be passed through a proxy. Could this be part of the problem?

Using the App.Config file does not work with this content either

 <configuration> <system.net> <defaultProxy enabled="true" useDefaultCredentials="true"> </defaultProxy> </system.net> </configuration> 
+6
source share
4 answers

I had the same problem maybe two weeks ago !! HTTP errors 407. I tried both sentences, set the network credentials, and added the default proxy line to app.config, but it didn’t work. Strange, because I did not have this problem in a vb application that I wrote last year that did the same, go to the website and download the source as a string. When I did this, I ran into this problem, but adding the defaultProxy line to App.config! Weird

This is how I got around this problem.

I do not create a proxy, I assign the default (pulled from app.config) proxy webRequest attribute. Then I guarantee that the "UseDefaultCredentials" parameter is set to "true".

 HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest); request.Proxy = WebRequest.DefaultWebProxy; request.UseDefaultCredentials = true; 

I added this to app.config (same as above).

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

Here is a kicker (and I would like to explain why this solved my problem). Right after creating the request and assigning the proxy with the correct credentials ... I had to create a cookie container and assign it to my web request. Seriously, I don’t know why this solved this problem because I didn’t have to do this before.

 CookieContainer cc = new CookieContainer(); request.CookieContainer = cc; 

Hope this helps you.

Here's the full code (minus the app.config line):

  HttpWebRequest request = (HttpWebRequest.Create(m.Url) as HttpWebRequest); request.Proxy = WebRequest.DefaultWebProxy; request.UseDefaultCredentials = true; CookieContainer cc = new CookieContainer(); request.CookieContainer = cc; HttpWebResponse response = (request.GetResponse() as HttpWebResponse); 
+6
source

Since klugerama already suggested you use CredentialCache.DefaultNetworkCredentials .

It contains network credentials , even if they appear empty .

I liked to insert another answer from this post on SO with a great explanation:

The NetworkCredential returned from CredentialCache.DefaultCredential is just a placeholder .... The internal API checks this type to see if integrated authentication should be used.

So you should check:

  • Is there a problem or do you think that due to the lack of username and password?
  • Check which account is used as the "default network credentials" (check the Credential Manager control panel).
+1
source

Check the app.config file (or web.config ) if it exists. If these proxy settings will be applied in all applications, make sure that this file contains the following (if necessary, create it):

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

You do not need to have anything in your code; all instances of WebRequest will simply use a proxy.

If the proxy settings will not be applied in all applications, make sure that these parameters are not in the .config file above, and be sure to set them in the code as necessary.

+1
source

It worked for me. Of course you have to add the url. Snip code - it was extracted from an application that I made that sends XML Soap requests and responses to one company’s web service that should be used while on other Cisco Any-Connect networks, or beyond.

 public HttpWebRequest CreateWebRequest() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url"); IWebProxy proxy = request.Proxy; if (proxy != null) { string proxyuri = proxy.GetProxy(request.RequestUri).ToString(); request.UseDefaultCredentials = true; request.Proxy = new WebProxy(proxyuri, false); request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; } return request; } 

App.config File:

 <system.net> <defaultProxy enabled="true" useDefaultCredentials="true"> </defaultProxy> </system.net> 
0
source

All Articles