Proxy Basic Authentication in C#: HTTP 407 error

132,557

Solution 1

here is the correct way of using proxy along with creds..

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

IWebProxy proxy = request.Proxy;                    
if (proxy != null)
{
    Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
}
else
{
    Console.WriteLine("Proxy is null; no proxy will be used");
}

WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://20.154.23.100:8888");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the 
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("userName", "password");
request.Proxy = myProxy;

Thanks everyone for help... :)

Solution 2

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>

Solution 3

I was getting a very similar situation where the HttpWebRequest wasn't picking up the correct proxy details by default and setting the UseDefaultCredentials didn't work either. Forcing the settings in code however worked a treat:

IWebProxy proxy = myWebRequest.Proxy;
if (proxy != null) {
    string proxyuri = proxy.GetProxy(myWebRequest.RequestUri).ToString();
    myWebRequest.UseDefaultCredentials = true;
    myWebRequest.Proxy = new WebProxy(proxyuri, false);
    myWebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
}

and because this uses the default credentials it should not ask the user for their details.

Solution 4

This problem had been bugging me for years the only workaround for me was to ask our networks team to make exceptions on our firewall so that certain URL requests didn't need to be authenticated on the proxy which is not ideal.

Recently I upgraded the project to .NET 4 from 3.5 and the code just started working using the default credentials for the proxy, no hardcoding of credentials etc.

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

Solution 5

I had a similar problem due to a password protected proxy server and couldn't find much in the way of information out there - hopefully this helps someone. I wanted to pick up the credentials as used by the customer's browser. However, the CredentialCache.DefaultCredentials and DefaultNetworkCredentials aren't working when the proxy has it's own username and password even though I had entered these details to ensure thatInternet explorer and Edge had access.

The solution for me in the end was to use a nuget package called "CredentialManagement.Standard" and the below code:

using WebClient webClient = new WebClient();    
var request = WebRequest.Create("http://google.co.uk");
var proxy = request.Proxy.GetProxy(new Uri("http://google.co.uk"));

var cmgr = new CredentialManagement.Credential() { Target = proxy.Host };
if (cmgr.Load())
{
    var credentials = new NetworkCredential(cmgr.Username, cmgr.Password);
    webClient.Proxy.Credentials = credentials;
    webClient.Credentials = credentials;
}

This grabs credentials from 'Credentials Manager' - which can be found via Windows - click Start then search for 'Credentials Manager'. Credentials for the proxy that were manually entered when prompted by the browser will be in the Windows Credentials section.

Share:
132,557

Related videos on Youtube

rplusg
Author by

rplusg

"Pro"grammer with lots of interest and experience in OS(Windows), storage, networking. And strongly believes c++ is the fastest. Currently working on C++\VC++ on Windows7, SilverLight\C# on WindowsPhone7, WindowsPhone8, Windows RT

Updated on July 09, 2022

Comments

  • rplusg
    rplusg almost 2 years

    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?

    • Darin Dimitrov
      Darin Dimitrov about 12 years
      What authentication scheme does your proxy server require? Basic? NTLM?
    • rplusg
      rplusg about 12 years
      basic, that is why i used Basic in creds cache...
  • tomfanning
    tomfanning over 11 years
    This is only correct if you want to force your user to discover their proxy server address and port number, and type their username and password into your application, and your application to store it somewhere (either in the clear or using necessarily reversible encryption) - hardly ideal - check my answer for a cleaner method that will probably work in most scenarios.
  • Smeiff
    Smeiff over 9 years
    This worked in my console application however it does not support HTTPS in my .NET applicaiton. The solution from tomfanning does work. Thanks!
  • Darren Gosbell
    Darren Gosbell over 9 years
    Storing the proxy address and credentials in your app is not a good idea - either of the other two answers are better than this
  • coder3521
    coder3521 over 8 years
    Ya this is better way . as you need not to add the credentials in your code . +1 .. works seamlessly
  • Azaz ul Haq
    Azaz ul Haq about 7 years
    @tomfanning, I believe this solution wont work if proxy server credentials are different than user's default credentials. For example if proxy server permits only specific accounts, and each account has its own password. In that case Using Deafult Credentials will not work.
  • tomfanning
    tomfanning about 7 years
    5 years on... yes, of course this solution won't work if default credentials (i.e. your Windows account) are not the credentials required by your proxy.