Category Archives: C#

How to Get your .Net Framework 4 C# application to use a proxy (with NTLM Authentication)

 I’d been working on an .net framework application that accessed a web resource internal to my organisation – ie, on the internal network where I work. There was no need to use a proxy. Then all of a sudden I was asked to re-write the code for this application to enable it to use a proxy (one that did NTLM authentication) as the resource was being scrapped and we were to buy other resources and access it over the web [via our proxy].

Here’s how I did it. Call this function from your class at a point before it starts making http requests and you should be okay. NB: this code works on our wholly Microsoft Windows network with NTLM authentication everywhere including the proxy server.

This works for me. Haven’t tried it with anything other than IE + XP + IAS though.

Best of luck.

static void setProxy()
{
    WebProxy proxy = (WebProxy)WebProxy.GetDefaultProxy();
    if(proxy.Address != null)
    {
        proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy.Address, proxy.BypassProxyOnLocal, proxy.BypassList, proxy.Credentials);
    }
}
I think this was the final code snippet.. Anyway, Give it a blast and let me know how you get on if you want to. Feel free to leave a comment. If you like it then link to it, if you don’t then tell a colleague.. 🙂

Seamus