locked
Proxy authentication with fallback (HttpWebRequest) RRS feed

  • Question

  • We have an application which uses System.Net.HttpWebRequest to load images or other files from webservers. If the webserver is outside the corporate network we need a proxy authentication. Normaly, this works fine with the default credentials (over NTLM). But in our case we are using a service account, which is not accepted by the proxy. After the NTLM authentication fails, the proxy sends a 407 error and gives you the possibility to authenticate with Basic authentication. But instead of trying to authenticate with the credentials for basic authentication, the library closes the connection and throws an exception (System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.).

    When I open the same file with a browser (IE/Chrome) it works. After the NTLM authentication fails the browser asks for the credentials and authenticates you with basic authentication.

    How can I achieve the same thing with HttpWebRequest?

    The proxy doesn't accept basic authentication unless you tried to authenticate by NTLM before.

    Here is my code:

    var destUri = new Uri("http://google.com");
                var proxyUri = new Uri("proxyUri");
    
                var request = (HttpWebRequest)WebRequest.Create(destUri);
    
                var credentialsNtlm = new NetworkCredential("username1", "password1", "domain");
                var credentialsBasic = new NetworkCredential("username2", "password2");
    
                var credCache = new CredentialCache();
                credCache.Add(proxyUri, "ntlm", credentialsNtlm);
                credCache.Add(proxyUri, "basic", credentialsBasic);
    
                request.Proxy.Credentials = credCache;
    
                try
                {
                    var resp = request.GetResponse();
    
                    Console.WriteLine("Success");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: {0}", e);
                }
    
                Console.ReadLine();

    Monday, September 1, 2014 10:37 AM

All replies

  • Hello Team DGV,

    I am trying to involve someone experienced to help look into this case, this may take some time and as soon as we get any result, we will tell you.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 2, 2014 7:15 AM