Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proxy Basic Authentication in C#: HTTP 407 error

I am working with a proxy that requires authentication, i.e., in a browser if I try to open a page it will immediately ask for credentials. I supplied same credentials in my program but it fails with HTTP 407 error.

Here is my code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);  IWebProxy proxy = WebRequest.GetSystemWebProxy(); CredentialCache cc = new CredentialCache(); NetworkCredential nc = new NetworkCredential();  nc.UserName = "userName"; nc.Password = "password"; nc.Domain = "mydomain"; cc.Add("http://20.154.23.100", 8888, "Basic", nc); proxy.Credentials = cc; //proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Proxy = proxy; request.Proxy.Credentials = cc; request.Credentials = cc; request.PreAuthenticate = true; 

I have tried every possible thing but seem like I am missing something. Is it something like, I have to make two requests? First with out credentials and once I hear back from server about need for credentials, make same request with credentials?

like image 847
rplusg Avatar asked Mar 07 '12 14:03

rplusg


People also ask

What is basic proxy authentication?

Proxy Authentication enables you to configure the authentication method used by the proxy. This determines how client machines are validated when accessing the Internet. Proxy Authentication must be enabled to be able to create new policies for users or groups. By default, Proxy Authentication is disabled.


1 Answers

This method may avoid the need to hard code or configure proxy credentials, which may be desirable.

Put this in your application configuration file - probably app.config. Visual Studio will rename it to yourappname.exe.config on build, and it will end up next to your executable. If you don't have an application configuration file, just add one using Add New Item in Visual Studio.

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <system.net>     <defaultProxy useDefaultCredentials="true" />   </system.net> </configuration> 
like image 75
tomfanning Avatar answered Sep 24 '22 02:09

tomfanning