64

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)

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • 1
    See we have two ways to use proxy in .net application . First one is use proxy settings in web.config. Second one use webproxy class in code. In web.config you can't use network credentials (user and password). But in code you can use credentials. You can learn more about proxy here - http://goo.gl/bLDAHp – virender Sep 18 '15 at 07:35

10 Answers10

102

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

Bellarmine Head
  • 3,397
  • 2
  • 22
  • 31
  • 1
    Not sure if you saw my original post, but I had stated that using app.config was not allowed in my deployment... or else I would have done that. – Brian Genisio Nov 30 '11 at 10:35
  • 21
    I know Brian, but taking your question "How should I set the default proxy to use default credentials?", I think my answer will help a lot of people since most folks will be able to set their config file's contents. – Bellarmine Head Dec 05 '11 at 09:36
  • 1
    Basically Microsoft's default setting of "false" is wrong and has caused and continues to cause an awful lot of confusion. Not many people know how to fix the problem, either in code or in config - I know this from first hand experience. If you agree, please vote up my "User Voice" suggestion to change this in future versions of .NET; see above for the link. Thanks. – Bellarmine Head Dec 05 '11 at 09:39
  • Noob question: How does this setting affect those that are not behind any proxy? – sjlewis Jan 25 '12 at 14:02
  • 5
    sjlewis, this setting does not affect those who aren't behind any proxy. The setting is only consulted when .NET code needs to reach out across a network (typically the web), but encounters a proxy server that requires credentials. If you're shipping a desktop app that may or may not be run behind an authenticating proxy, better to ship the app.config file with this setting in, to avoid potential problems. If Microsoft changed the default, that wouldn't be necessary tho. :) – Bellarmine Head Jan 25 '12 at 14:33
  • This works like a charm! I am working with Amazon S3 SDK v1.5 and required a way to tell the WebProxy object created by S3 library to use default credentials.The old version of the SDK had a public property that could set it to use default credentials but the new one did not have this property. However, adding the system.web section as advised by @AndrewWebb solved the issue!! – Sudhanshu Mishra Oct 17 '12 at 06:30
  • For more details on how to make this change for use with `svcutil.exe`, see this post: http://stackoverflow.com/questions/15124350/how-to-run-svcutil-exe-from-behind-authenticating-proxy. Thanks @AndrewWebb! I've upvoted your VS UserVoice issue too! – kmote Feb 27 '13 at 23:17
  • I had this setting in web.config but it required the identity of the IIS app pool to be a valid domain user in order to access the proxy - the `NetworkService` identity wasn't sufficient. – David Clarke Feb 04 '15 at 21:56
  • This doesn't seem to be working for me in a console app using HttpClient. Does it work with HttpClient? – NickG Feb 18 '16 at 12:12
  • @NickG: having this configuration in the app.config file of a console app that uses HttpClient absolutely does work, even though I do remember finding that HttpClient doesn't strictly need it. HttpWebRequest needs it, but - I do recall - HttpClient doesn't. But having the config for HttpClient certainly doesn't break anything, it's just extra insurance. If you are having trouble punching thru an authenticating web proxy with or without this config, then I suspect a deeper issue is at work. E.g. follow my `User Voice` link (above) and look at Divakar's comment from last July. – Bellarmine Head Feb 19 '16 at 16:47
  • for me this works in .net but gives an error of unknown property in mono – ColmanJ Nov 03 '16 at 09:07
57

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.

Martin Hollingsworth
  • 7,249
  • 7
  • 49
  • 51
  • I agree that it FEELS like I shouldn't need to do this, but I do. If I don't do this, I get a 407 failure on the default proxy authentication. If I DO this, then my client can get through the proxy. – Brian Genisio Nov 19 '08 at 13:14
  • Is it possibly that you have a element in your .config file? If so get rid of it and try again. – Martin Hollingsworth Nov 21 '08 at 02:21
  • The setting in the config file was the only thing that got it to work... but I was just playing around with it. I don't have access to .config in my environment at all, so this isn't an option for me. – Brian Genisio Nov 28 '08 at 14:04
  • 1
    WebRequest.Create will generate a request with the default proxy, but this proxy will have UseDefaultCredentials set to false by default. To use the default credentials set this to true. – Martin Doms Nov 23 '11 at 00:19
19

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.

Thariq Nugrohotomo
  • 733
  • 1
  • 6
  • 12
  • 2
    This is the only answer that helped me with my problem. It essentially does what the accepted answer does, but without the app.config. In our winrt windows8.1 app we cannot add app.config in the deployment, it breaks the app for some reason. – evilkos Sep 26 '16 at 08:00
5

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;
Taryn
  • 242,637
  • 56
  • 362
  • 405
André
  • 51
  • 1
  • 1
3

Seems like in some newer Application the Configuration is different, as i've seen on this Question 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

Community
  • 1
  • 1
2

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).

smack
  • 113
  • 7
1

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

jyz
  • 6,011
  • 3
  • 29
  • 37
0

This is the new suggested method.

WebRequest.GetSystemWebProxy();
Jim Scott
  • 2,493
  • 1
  • 18
  • 16
  • 3
    Care to clarify what the 'new' ness is, isnt it in since v2 like the stuff in the other answer? Can you expand on how this answer is different and/or better than the other? – Ruben Bartelink Jan 18 '11 at 00:24
  • Akshinthala: The question states that the method he is using is deprecated WebProxy.GetDefaultProxy() and was asking what he should be using instead. If you go to the documentation for that method http://msdn.microsoft.com/en-us/library/system.net.webproxy.getdefaultproxy(v=vs.110).aspx you will see that it lists the updated method that should now be used. Applications should use the WebRequest.DefaultWebProxy property and the WebRequest.GetSystemWebProxy method instead of the GetDefaultProxy method. – Jim Scott Aug 15 '14 at 16:16
0

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.

Tolga
  • 2,643
  • 1
  • 27
  • 18
-3

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.

Rtisatto
  • 767
  • 8
  • 7