How should I set the default proxy to use default credentials?
Asked Answered
B

10

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)

Byrdie answered 18/11, 2008 at 20:0 Comment(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 - goo.gl/bLDAHpGirandole
O
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.

Obligor answered 19/11, 2008 at 1:10 Comment(4)
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.Byrdie
Is it possibly that you have a <proxy> element in your .config file? If so get rid of it and try again.Obligor
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.Byrdie
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.Tamaratamarack
G
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

Goggin answered 18/11, 2011 at 10:20 Comment(11)
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.Byrdie
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.Goggin
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.Goggin
Noob question: How does this setting affect those that are not behind any proxy?Eipper
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. :)Goggin
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!!Periodontics
For more details on how to make this change for use with svcutil.exe, see this post: #15124850. Thanks @AndrewWebb! I've upvoted your VS UserVoice issue too!Undertint
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.Inflammatory
This doesn't seem to be working for me in a console app using HttpClient. Does it work with HttpClient?Rossanarosse
@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.Goggin
for me this works in .net but gives an error of unknown property in monoDuley
O
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.

Obligor answered 19/11, 2008 at 1:10 Comment(4)
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.Byrdie
Is it possibly that you have a <proxy> element in your .config file? If so get rid of it and try again.Obligor
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.Byrdie
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.Tamaratamarack
S
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.

Saree answered 30/9, 2013 at 5:2 Comment(1)
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.Golgotha
E
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;
Emblements answered 11/7, 2012 at 9:44 Comment(0)
V
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

Vitiated answered 25/1, 2017 at 9:44 Comment(1)
When I put this code in my App.Config I get a crash in KERNELBASE.dll with no error messagePurcell
A
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).

Avionics answered 11/12, 2013 at 10:24 Comment(0)
L
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

Loop answered 17/4, 2013 at 18:44 Comment(0)
M
0

This is the new suggested method.

WebRequest.GetSystemWebProxy();
Magnesite answered 17/1, 2011 at 22:30 Comment(2)
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?Primula
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 msdn.microsoft.com/en-us/library/… 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.Magnesite
L
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.

Labourer answered 12/11, 2020 at 6:33 Comment(0)
B
-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.

Blackstock answered 11/11, 2014 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.