78

I'm getting this error when I call a web service:

"The remote server returned an error: (407) Proxy Authentication Required".

I get the general idea and I can get the code to work by adding

myProxy.Credentials = NetworkCredential("user", "password", "domain");

or by using DefaultCredentials in code. My problem is that the call to the web service works in production without this.

It seems like there is a non code solution involving Machine.config, but what is it? At the moment I can't get to the production box's machine.config file to see what that looks like. I tried updating my machine.config as follows, but I still get the 407 error.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <bypasslist>
            <clear />
        </bypasslist>
        <proxy proxyaddress="myproxy:9000"
               usesystemdefault="false"
               bypassonlocal="true"
               autoDetect="False" />
    </defaultProxy>
</system.net>
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
chris
  • 821
  • 1
  • 6
  • 5

7 Answers7

146

Just add this to config

<system.net>
    <defaultProxy useDefaultCredentials="true" >
    </defaultProxy>
</system.net>
Seçkin Durgay
  • 2,758
  • 4
  • 27
  • 36
  • For those interested, this was exactly what I needed inside a NetSuite console / web services application from (what seems to be) a firewall / proxy constraint. – dah97765 Jan 04 '16 at 19:30
  • 3
    Best answer, make it a configuration detail not a code problem. – mccainz Jan 14 '16 at 20:02
  • 1
    This still works in Asp.net core 2.0. I had to add a web.config file purely to handle this. – jmdon Mar 14 '18 at 15:35
  • 1
    Mine is a C# console application working with AWS SDK to access S3 buckets. Add this to my app.config in my console application solved the issue – Ziggler Dec 28 '21 at 18:22
55

In following code, we don't need to hard code the credentials.

service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
  • This worked perfect for me. If you find yourself not wanting to hardcode you username and password try this option first. – sapbucket Jun 05 '13 at 21:00
  • Thanks that worked. However, are there security risks with doing it this way vs. the method shown by mehmet emin? With adding network credentials? Or would using network credentials give more permissions, and introduce a security risk? – Bryan Aug 27 '15 at 14:44
  • 1
    @Bryan Only problem with 'Mehmet Emin' code is hard-coding of credentials. In real worlds, password is changed every month due to security threats, so either make them configurable in config file. so it's always good to follow my stuff or I like Seckin way also. – Romil Kumar Jain Aug 28 '15 at 14:34
  • Worked as expected. Thanks – Anshuman Jasrotia Nov 29 '17 at 11:34
  • 1
    @kucluk You have created object of service proxy, so put this code before calling any method of service. – Romil Kumar Jain Feb 18 '19 at 07:53
20

Check with your firewall expert. They open the firewall for PROD servers so there is no need to use the Proxy.

Thanks your tip helped me solve my problem:

Had to to set the Credentials in two locations to get past the 407 error:

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

and voila!

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Werner du Toit
  • 201
  • 2
  • 2
7

Probably the machine or web.config in prod has the settings in the configuration; you probably won't need the proxy tag.

<system.net>
    <defaultProxy useDefaultCredentials="true" >
        <proxy usesystemdefault="False"
               proxyaddress="http://<ProxyLocation>:<port>"
               bypassonlocal="True"
               autoDetect="False" />
    </defaultProxy>
</system.net>
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Oscar Cabrero
  • 4,168
  • 8
  • 29
  • 49
  • Thanks, I tried this and I still get the "(407) Proxy Authentication Required" error. Does adding the section you list stop the error for you? The only thing that works for sure on my local box is to set the myProxy.Credentials property in code. – chris Apr 06 '10 at 19:37
4
HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;

webRequest.Proxy = WebRequest.DefaultWebProxy;

webRequest.Credentials = new NetworkCredential("user", "password");

webRequest.Proxy.Credentials = new NetworkCredential("user", "password");

It is successful.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
3

Thought of writing this answer as nothing worked from above & you don't want to specify proxy location.

If you're using httpClient then consider this.

HttpClientHandler handler = new HttpClientHandler();

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
handler.Proxy = proxy;

var client = new HttpClient(handler);
// your next steps...

And if you're using HttpWebRequest:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri + _endpoint);

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = proxy;

Kind referencce: https://medium.com/@siriphonnot/the-remote-server-returned-an-error-407-proxy-authentication-required-86ae489e401b

nirav
  • 373
  • 3
  • 7
  • 20
2

I had a similar proxy related problem. In my case it was enough to add:

webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");
KoturB
  • 155
  • 4
  • 12