Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IE proxy server settings and its credentials in .Net Application

How to use the saved proxy settings and credentials as default for HttpWebRequests? The proxy settings are accessible and used but not the credentials:

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

Are there any permissions on using the credentials?

It works with passing the credentials via NetworkCredential instance:

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

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

Edit:

I am using a third party lib creating and calling the HttpWebRequests which should be passed through the proxy. Could that be part of the problem?

Using the App.Config file with this content doesn't work either

<configuration>
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
    </defaultProxy>
  </system.net>
</configuration>
like image 871
Till Lorentzen Avatar asked Feb 24 '14 10:02

Till Lorentzen


People also ask

How do I use my proxy username and password?

Setting up the credentials for the Proxy Server: In Windows 10 menu, go to Settings (WinKey+I) and search for "Credential Manager". Under Windows Credentials, add a new entry for Windows Credentials. Enter the Proxy Server address (without the port number), your domain user name and the password.


2 Answers

I was having this exact same issue maybe two weeks ago!! HTTP 407 errors. I tried both suggestions, set network credentials and add the default proxy line in the app.config but neither worked. Strange too because I didn't have this issue on a vb application I wrote last year that did the exact same thing, go out to a web site and download the source as a string. When I did it then, I ran into this issue but adding the defaultProxy line in App.config worked! Weird.

This is how I was able to get around the issue.

I don't create a proxy, I assign the default one (pulled from app.config) to the webRequest's proxy attribute. I then ensure that "UseDefaultCredentials" is set to "true".

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

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

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

Here's the kicker (and I wish I could explain why this solved my issue). Right after I create the request and assign 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 issue because I didn't have to do it before.

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

I hope this helps you.

Here's the complete code (minus the app.config lines):

        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);
like image 80
Nick H. Avatar answered Nov 03 '22 02:11

Nick H.


As klugerama already suggested you should use CredentialCache.DefaultNetworkCredentials.

This contains the network credentials even when they seem empty.

I had like to paste another answer from this post on SO with an excellent explanation:

The NetworkCredential returned from CredentialCache.DefaultCredential is just a placeholder. ... Internal API check for this type to see if integrated authentication should be used or not.

So the things you should check is:

  • Is there really a problem or do you think there is because of the missing username and password?
  • Check which account is used as 'default network credentials' (check the Credential Manager control panel)
like image 34
Patrick Hofman Avatar answered Nov 03 '22 00:11

Patrick Hofman