Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to specify proxy credentials in your web.config?

I need to configure a website to access a webservice on another machine, via a proxy. I can configure the website to use a proxy, but I can't find a way of specifying the credentials that the proxy requires, is that possible? Here is my current configuration:

<defaultProxy useDefaultCredentials="false">     <proxy usesystemdefault="true" proxyaddress="<proxy address>" bypassonlocal="true" /> </defaultProxy> 

I know you can do this via code, but the software the website is running is a closed-source CMS so I can't do this.

Is there any way to do this? MSDN isn't helping me much..

like image 556
spmason Avatar asked Oct 09 '08 11:10

spmason


People also ask

How do I change proxy credentials?

Setting up the credentials for the Proxy Server: In Windows 10 menu, go to Settings (WinKey+I) and search for "Credential Manager". Under Windows Credentials, add a new entry for Windows Credentials. Enter the Proxy Server address (without the port number), your domain user name and the password.

What is proxy config?

A proxy auto-config (PAC) file defines how web browsers and other user agents can automatically choose the appropriate proxy server (access method) for fetching a given URL. A PAC file contains a JavaScript function FindProxyForURL(url, host) .


1 Answers

Yes, it is possible to specify your own credentials without modifying the current code. It requires a small piece of code from your part though.

Create an assembly called SomeAssembly.dll with this class :

namespace SomeNameSpace {     public class MyProxy : IWebProxy     {         public ICredentials Credentials         {             get { return new NetworkCredential("user", "password"); }             //or get { return new NetworkCredential("user", "password","domain"); }             set { }         }          public Uri GetProxy(Uri destination)         {             return new Uri("http://my.proxy:8080");         }          public bool IsBypassed(Uri host)         {             return false;         }     } } 

Add this to your config file :

<defaultProxy enabled="true" useDefaultCredentials="false">   <module type = "SomeNameSpace.MyProxy, SomeAssembly" /> </defaultProxy> 

This "injects" a new proxy in the list, and because there are no default credentials, the WebRequest class will call your code first and request your own credentials. You will need to place the assemble SomeAssembly in the bin directory of your CMS application.

This is a somehow static code, and to get all strings like the user, password and URL, you might either need to implement your own ConfigurationSection, or add some information in the AppSettings, which is far more easier.

like image 124
Jérôme Laban Avatar answered Sep 26 '22 10:09

Jérôme Laban