How to pass credentials in defaultProxy config setting?

Posted on

Problem :

A client is unable to use my webpart because he is behind a proxy server and they need to specify a username and password to get past the proxy. I have this in my config file right now:

      <proxy usesystemdefault="False" proxyaddress="" bypassonlocal="True"   />

Is there a way to supply a username and password to this proxy setting?

Solution :

I’m not aware of a way to do this in defaultProxy section of web.config, but you can definitely do it from code. Try this:

// Get proxy server info from AppSettings section of Web.Config
var proxyServerAddress = ConfigurationManager.AppSettings[ "proxyServerAddress" ];
var proxyServerPort = ConfigurationManager.AppSettings[ "proxyServerPort" ];

// Get proxy with default credentials 
WebProxy proxy =new WebProxy(proxyServerAddress, proxyServerPort);
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials();

Web.Config (configuration section):

  <add key="proxyServerAddress" value="" />
  <add key="proxyServerPort" value="8080" />

And then assign proxy to the webClient you are using in your webPart.


If I had done more homework, I would have realized your problem could have been fixed with one attribute: useDefaultCredentials=”true”

    <defaultProxy useDefaultCredentials="true"> 
        <proxy usesystemdefault="False" proxyaddress="" bypassonlocal="True" />  

Leave a Reply

Your email address will not be published. Required fields are marked *