26

In net 4.5 we are working with proxy like this:

<system.net>
    <!-- -->
    <defaultProxy enabled="true" useDefaultCredentials="false">
        <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False" />
        <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" />
    </defaultProxy>

    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
        <servicePointManager expect100Continue="false" />
    </settings>
</system.net>

but in asp.net core or test we can't found a solution like the above Could someone please help me?

I really appreciate your help

Thanks, Regards

6 Answers 6

17

You can set the proxy explicitly in the web.config as environment variables, as well as the domains it should skip. For example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Your.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
        <environmentVariables>
          <environmentVariable name="http_proxy" value="http://yourproxy.ins.local"/>
          <environmentVariable name="https_proxy" value="http://yourproxy.ins.local"/>
          <environmentVariable name="no_proxy" value=".local,.applicationinsights.azure.com,.applicationinsights.microsoft.com,.services.visualstudio.com"/>
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
Sign up to request clarification or add additional context in comments.

5 Comments

This works great! You can also set the environment variables server-wide by setting them globally on the server inside control panel.
This worked great in .net6
can i do the same for a proxy which requires user and password?
@Bouke Woudstra can i do the same for a proxy which requires user and password
Can anyone suggest me how to add same settings as above in net6.0 so that i could test in fiddler ?
9

Whilst manually setting the proxy works when it's possible to use a HttpClientHander, defaulting all requests to do so without code, like you could do in the .NET Framework is currently not possible. Which is bummer if you're using a library that doesn't expose this functionality.

Thankfully, from .NET Core 3.0, this will be possible simply by setting environment variables (i.e. behaves exactly as Linux has worked forever): https://github.com/dotnet/corefx/issues/37187

Alternatively, https://github.com/dotnet/corefx/issues/36553 added a new static DefaultWebProxy property on HttpClient that will allow you to accomplish the same thing via code. This will also be available in Core 3.0.

2 Comments

It is not working for me when I try to set the proxy for Fiddler url. I set the "http_proxy" environment variable with the value "127.0.0.1:8888". Fiddler is still not capturing the traffic for the dotnet core application.
I tried above solutions but still not working, I am trying to call interfax service but its not working successfully
8

To use an HTTP proxy in .net core, you have to implement IWebProxy interface.This is from the System.Net.Primitives.dll assembly. You can add it to project.json if not already there

e.g.

"frameworks": {
    "dotnet4.5": {
      "dependencies": {
        "System.Net.Primitives": "4.3.0"
      }
    }
}

Implementation is very trivial

public class MyHttpProxy : IWebProxy
    {

        public MyHttpProxy()
        {
           //here you can load it from your custom config settings 
            this.ProxyUri = new Uri(proxyUri);
        }

        public Uri ProxyUri { get; set; }

        public ICredentials Credentials { get; set; }

        public Uri GetProxy(Uri destination)
        {
            return this.ProxyUri;
        }

        public bool IsBypassed(Uri host)
        {
            //you can proxy all requests or implement bypass urls based on config settings
            return false; 

        }
    }


var config = new HttpClientHandler
{
    UseProxy = true,
    Proxy = new MyHttpProxy()
};

//then you can simply pass the config to HttpClient
var http = new HttpClient(config)

checkout https://msdn.microsoft.com/en-us/library/system.net.iwebproxy(v=vs.100).aspx

4 Comments

Thanks yo much. How i can do this for wcf client?
Hi. I need use defaultProxy because i have a client wcf that i am accesing throught a proxy with the ip address validated on the whitelist of my provider.
@MarceloOliveto are you using client-oriented WCF libraries in .net core ? looks like it still does not support proxying,there is an issue opened for it github.com/dotnet/wcf/issues/1592
@MarceloOliveto Did you ever find a solution for this?
1

You should use a middleware. Did you have a look at this one:

https://github.com/aspnet/Proxy

there's a 'samples' folder: https://github.com/aspnet/Proxy/tree/dev/samples/Microsoft.AspNetCore.Proxy.Samples

Other resources on the topic: http://josephwoodward.co.uk/2016/07/proxying-http-requests-asp-net-core-using-kestrel

http://overengineer.net/creating-a-simple-proxy-server-middleware-in-asp-net-core

Does a middleware works for you?

2 Comments

Hi. I need use defaultProxy because i have a client wcf that i am accesing throught a proxy with the ip address validated on the whitelist of my provider.
That's the wrong type of proxy. That makes your app a proxy, not make your app use a proxy.
1

In a debug session it can be achieved setting the proxy environment variable in the \Properties\launchSettings.json file:

    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "dashboard",
      "applicationUrl": "https://localhost:7036;http://localhost:5085",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ALL_PROXY" : "http://localhost:8888" // <- proxy 
      }
    }

Comments

-4

Another incompleted work around is:

After you deploy .NET Core app to a web server(mine is IIS). There is actually a web.config file in the root folder.

Manually add

 <system.net>
 <defaultProxy useDefaultCredentials="true">
 </defaultProxy>
 </system.net>

or your specific setting will do the magic

Only works on the server though. Your local build still won't work.

1 Comment

This doesn't work for later .net versions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.