How should I set the default proxy to use default credentials?

.NetProxy

.Net Problem Overview


The following code works for me:

var webProxy = WebProxy.GetDefaultProxy();
webProxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = webProxy;

Unfortunately, WebProxy.GetDefaultProxy() is deprecated. What else should I be doing?

(using app.config to set the defaultProxy settings is not allowed in my deployment)

.Net Solutions


Solution 1 - .Net

For those who, unlike Brian Genisio, are able to set the contents of their application's config file:- don't do anything in code. Instead add this to your app.config / web.config.

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

Really and truly the default for using the default credentials should be "true"; I've seen this issue confuse so many people - developers, users, IT guys.

For more info see here:- http://sticklebackplastic.com/post/2007/01/26/Poxy-proxies.aspx

UPDATE: I've created this issue/idea for Microsoft to change the default of useDefaultCredentials from false to true so that this whole problem goes away and .NET apps "just work"; please vote it up if you agree:
http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2397357-fix-it-so-that-net-apps-can-access-http-thru-auth

Solution 2 - .Net

From .NET 2.0 you shouldn't need to do this. If you do not explicitly set the Proxy property on a web request it uses the value of the static WebRequest.DefaultWebProxy. If you wanted to change the proxy being used by all subsequent WebRequests, you can set this static DefaultWebProxy property.

The default behaviour of WebRequest.DefaultWebProxy is to use the same underlying settings as used by Internet Explorer.

If you wanted to use different proxy settings to the current user then you would need to code

WebRequest webRequest = WebRequest.Create("http://stackoverflow.com/");
webRequest.Proxy = new WebProxy("http://proxyserver:80/",true);

or

WebRequest.DefaultWebProxy = new WebProxy("http://proxyserver:80/",true);

You should also remember the object model for proxies includes the concept that the proxy can be different depending on the destination hostname. This can make things a bit confusing when debugging and checking the property of webRequest.Proxy. Call

webRequest.Proxy.GetProxy(new Uri("http://google.com.au")) to see the actual details of the proxy server that would be used.

There seems to be some debate about whether you can set webRequest.Proxy or WebRequest.DefaultWebProxy = null to prevent the use of any proxy. This seems to work OK for me but you could set it to new DefaultProxy() with no parameters to get the required behaviour. Another thing to check is that if a proxy element exists in your applications config file, the .NET Framework will NOT use the proxy settings in Internet Explorer.

The MSDN Magazine article Take the Burden Off Users with Automatic Configuration in .NET gives further details of what is happening under the hood.

Solution 3 - .Net

This will force the DefaultWebProxy to use default credentials, similar effect as done through UseDefaultCredentials = true.

WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

Hence all newly created WebRequest instances will use default proxy which has been configured to use proxy's default credentials.

Solution 4 - .Net

You may use Reflection to set the UseDefaultCredentials-Property from Code to "true"

System.Reflection.PropertyInfo pInfo = System.Net.WebRequest.DefaultWebProxy.GetType().GetProperty("WebProxy", 
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

((System.Net.WebProxy)pInfo.GetValue(System.Net.WebRequest.DefaultWebProxy, null)).UseDefaultCredentials = true;

Solution 5 - .Net

Seems like in some newer Application the Configuration is different, as i've seen on this Question https://stackoverflow.com/questions/9827976/how-to-authenticate-against-a-proxy-when-using-the-httpclient-class

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
         <proxy usesystemdefault="True" />
    </defaultProxy>
</system.net>

Also documented on https://msdn.microsoft.com/en-us/library/dkwyc043.aspx

Solution 6 - .Net

This thread is old, but I just recently stumbled over the defaultProxy issue and maybe it helps others.

I used the config setting as Andrew suggested. When deploying it, my customer got an error saying, there weren't sufficient rights to set the configuration 'defaultProxy'.

Not knowing why I do not have the right to set this configuration and what to do about it, I just removed it and it still worked. So it seems that in VS2013 this issue is fixed.

And while we're at it:

    WebRequest.DefaultWebProxy.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword");

uses the default proxy with your credentials. If you want to force not using a proxy just set the DefaultWebProxy to null (though I don't know if one wants that).

Solution 7 - .Net

In my deployment I can't use app.config neither to embed what Andrew Webb suggested.
So I'm doing this:

    IWebProxy proxy = WebRequest.GetSystemWebProxy();
    proxy.Credentials = CredentialCache.DefaultCredentials;
    
    WebClient wc = new WebClient();
    wc.UseDefaultCredentials = true;
    wc.Proxy = proxy;

Just in case you want to check my IE settings:

enter image description here

Solution 8 - .Net

This is the new suggested method.

WebRequest.GetSystemWebProxy();

Solution 9 - .Net

Most of the answers here use deprecated APIs. New way of doing this in Powershell 5/6/7 is:

[System.Net.WebRequest]::GetSystemWebProxy().Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;

You can put the above line in your Powershell 5 and Powershell 6/7 $Profile.AllUsersAllHosts.

Solution 10 - .Net

Is need in some systems set null the Proxy proprerty:

Net.WebRequest.DefaultWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials Dim request As WebRequest = WebRequest.Create(sRemoteFileURL) request.Proxy = Nothing

It's a bug.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian GenisioView Question on Stackoverflow
Solution 1 - .NetBellarmine HeadView Answer on Stackoverflow
Solution 2 - .NetMartin HollingsworthView Answer on Stackoverflow
Solution 3 - .NetThariq NugrohotomoView Answer on Stackoverflow
Solution 4 - .NetAndréView Answer on Stackoverflow
Solution 5 - .NetDaniel WeberView Answer on Stackoverflow
Solution 6 - .NetsmackView Answer on Stackoverflow
Solution 7 - .NetjyzView Answer on Stackoverflow
Solution 8 - .NetJim ScottView Answer on Stackoverflow
Solution 9 - .NetTolgaView Answer on Stackoverflow
Solution 10 - .NetRodrigo T.View Answer on Stackoverflow