C# asp.net核心默认代理

C# asp.net核心默认代理,c#,asp.net,asp.net-core,asp.net-core-mvc,asp.net-core-middleware,C#,Asp.net,Asp.net Core,Asp.net Core Mvc,Asp.net Core Middleware,在net 4.5中,我们使用的代理如下: <system.net> <!-- --> <defaultProxy enabled="true" useDefaultCredentials="false"> <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False"

在net 4.5中,我们使用的代理如下:

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

但是在asp.net核心或测试中,我们找不到像上面这样的解决方案 谁能帮帮我吗

我真的很感谢你的帮助


谢谢,您应该使用中间件。你有没有看过这个:

有一个“示例”文件夹:

关于该主题的其他资源:


中间件是否适合您?

要在.net core中使用HTTP代理,您必须实现
IWebProxy
接口。该接口来自
System.net.Primitives.dll
程序集。如果还没有,您可以将其添加到
project.json

e、 g

实现非常简单

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)

签出

另一个未完成的解决方法是:

将.NET核心应用程序部署到web服务器后(我的是IIS)。根文件夹中实际上有一个web.config文件

手动添加

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

或者你的特定设置将发挥神奇作用


但只在服务器上工作。您的本地生成仍然无法工作。

当可以使用
HttpClientHander
时,手动设置代理可以工作,默认所有请求在没有代码的情况下执行此操作,就像您在.NET Framework中所做的那样,目前是不可能的。如果您使用的库没有公开此功能,那么这将非常糟糕

谢天谢地,在.NET Core 3.0中,只需设置环境变量即可实现这一点(即,其行为与Linux一直以来的工作方式完全相同):


或者,在
HttpClient
上添加一个新的静态
DefaultWebProxy
属性,允许您通过代码完成相同的任务。这也将在Core 3.0中提供。

您可以在web.config中将代理显式设置为环境变量,以及它应该跳过的域。例如:

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


非常感谢。我怎样才能为wcf客户做到这一点?嗨。我需要使用defaultProxy,因为我有一个客户端wcf,我正在通过一个代理访问该客户端wcf,该代理的ip地址在我的提供程序的白名单上经过验证。@MarceloOliveto您是否在.net core中使用面向客户端的wcf库?看起来它仍然不支持代理,有一个问题需要解决@MarceloOliveto你找到解决方案了吗?嗨。我需要使用defaultProxy,因为我有一个客户端wcf,我正在通过一个在我的提供商的白名单上验证了ip地址的代理来访问它。这是错误的代理类型。这使你的应用程序成为代理,而不是让你的应用程序使用代理。当我尝试为Fiddler url设置代理时,它对我无效。我将“http_proxy”环境变量的值设置为“”。Fiddler仍然没有捕获dotnet核心应用程序的流量。我尝试了上述解决方案,但仍然不起作用,我尝试调用interfax服务,但没有成功。这很好!您还可以通过在服务器内部控制面板上全局设置环境变量来设置服务器范围内的环境变量。
<?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>