63

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..

spmason
  • 4,048
  • 2
  • 24
  • 19

5 Answers5

107

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.

niarbrnd
  • 3
  • 2
Jérôme Laban
  • 5,224
  • 3
  • 20
  • 17
  • I'm going to mark this as the answer, because it looks as if it *should* work, but I'm still having issues connecting which seem to be down to ISA Server more than anything.. – spmason Oct 13 '08 at 20:09
  • We're not supposed to do +1, etc, but this is a really easy and elegant solution!! – Nick Patsaris Dec 17 '14 at 12:20
  • Any idea how to pass credentials to the assembly? I don't want to hardcode those information as the users should be able to change those later on without recompiling the code. – F.H. Nov 14 '18 at 14:38
20

While I haven't found a good way to specify proxy network credentials in the web.config, you might find that you can still use a non-coding solution, by including this in your web.config:

  <system.net>
    <defaultProxy useDefaultCredentials="true">
      <proxy proxyaddress="proxyAddress" usesystemdefault="True"/>
    </defaultProxy>
  </system.net>

The key ingredient in getting this going, is to change the IIS settings, ensuring the account that runs the process has access to the proxy server. If your process is running under LocalService, or NetworkService, then this probably won't work. Chances are, you'll want a domain account.

Scott Ferguson
  • 7,690
  • 7
  • 41
  • 64
9

You can specify credentials by adding a new Generic Credential of your proxy server in Windows Credentials Manager:

1 In Web.config

<system.net>    
<defaultProxy enabled="true" useDefaultCredentials="true">      
<proxy usesystemdefault="True" />      
</defaultProxy>    
</system.net>
  1. In Control Panel\All Control Panel Items\Credential Manager >> Add a Generic Credential

Internet or network address: your proxy address
User name: your user name
Password: you pass

This configuration worked for me, without change the code.

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
1

Directory Services/LDAP lookups can be used to serve this purpose. It involves some changes at infrastructure level, but most production environments have such provision

questzen
  • 3,260
  • 18
  • 21
0

Though its very late but it might be helpful for someone looking for solution to the same problem. I came across this question after having same problem. I am giving my solution to the problem, how I made it work. I created the proxy using using credentials like this,

public class MyProxy : IWebProxy
{
    public ICredentials Credentials
    {
        //get { return new NetworkCredential("user", "password"); }
        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;
    }
}

And then you have to register the HttpClient in the DI container like this and it will work perfectly.

services.AddHttpClient("Lynx", client =>
    {
        client.BaseAddress = new Uri(Configuration.GetSection("LynxUrl").Value);
    }).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { Proxy = new MyProxy()});
Malik
  • 65
  • 2
  • 9