6

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:

<system.net>
    <defaultProxy>        
      <proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True"   />
    </defaultProxy>
  </system.net>

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

Prabhu
  • 12,995
  • 33
  • 127
  • 210

1 Answers1

12

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):

<appSettings>
  <add key="proxyServerAddress" value="proxy.myhost.com" />
  <add key="proxyServerPort" value="8080" />
</appSettings>

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

EDIT:

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

<system.net>  
    <defaultProxy useDefaultCredentials="true"> 
        <proxy usesystemdefault="False" proxyaddress="http://127.0.0.1:8888" bypassonlocal="True" />  
    </defaultProxy>  
</system.net>
Byron Sommardahl
  • 12,743
  • 15
  • 74
  • 131
  • Thanks. But this would need to be the credentials of my client...not really sure I can get his password. Anyway I can get around this? – Prabhu Jul 22 '10 at 17:51
  • You think I can set up the address and port in the config and just add the proxy.Credentials line in the code? – Prabhu Jul 22 '10 at 18:05
  • 1
    Thanks! Is this not the same as doing this: – Prabhu Jul 22 '10 at 18:43
  • You're absolutely right. I wish I had thought of that before I went through all the "wheel-reinventing". – Byron Sommardahl Jul 22 '10 at 21:44
  • Thanks! What's the usesystemdefault=false for and why is it false? – Prabhu Jul 22 '10 at 22:10
  • 1
    I don't see how this answers the question. Where do we define the credentials? What if the logged in user's credentials do not match the needed credentials on the proxy server? – F.H. Nov 14 '18 at 14:08