asp.net核心defaultProxy

Mar*_*eto 12 c# asp.net asp.net-core-mvc asp.net-core 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" />
        <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" />
    </defaultProxy>

    <settings>
        <httpWebRequest useUnsafeHeaderParsing="true" />
        <servicePointManager expect100Continue="false" />
    </settings>
</system.net>
Run Code Online (Sandbox Code Playgroud)

但是在asp.net核心或测试中我们找不到像上面这样的解决方案有人可以帮帮我吗?

我非常感谢你的帮助

感谢和问候

Bou*_*tra 15

您可以在 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>
Run Code Online (Sandbox Code Playgroud)

  • 这在 .net6 中效果很好 (3认同)

Adr*_*ian 7

尽管可以在可能的情况下使用代理手动设置HttpClientHander,但默认情况下所有请求都无需代码即可使用,就像目前在.NET Framework中那样。如果您使用的库没有公开此功能,那真是太可惜了。

幸运的是,在.NET Core 3.0中,只需设置环境变量即可实现此目的(即,行为与Linux永远一样):https : //github.com/dotnet/corefx/issues/37187

另外,https://github.com/dotnet/corefx/issues/36553在上面添加了一个新的静态DefaultWebProxy属性HttpClient,使您可以通过代码完成相同的操作。Core 3.0也将提供此功能。


Roh*_*ith 5

要在.net核心中使用HTTP代理,您必须实现IWebProxyinterface.This来自System.Net.Primitives.dll程序集.你可以添加它,project.json如果还没有

例如

"frameworks": {
    "dotnet4.5": {
      "dependencies": {
        "System.Net.Primitives": "4.3.0"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

实施非常简单

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)
Run Code Online (Sandbox Code Playgroud)

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

  • @MarceloOliveto您有没有找到解决方案? (3认同)

Pet*_*nov 1

您应该使用中间件。你看过这个吗:

https://github.com/aspnet/Proxy

有一个“样本”文件夹: https://github.com/aspnet/Proxy/tree/dev/samples/Microsoft.AspNetCore.Proxy.Samples

有关该主题的其他资源: 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

中间件适合您吗?

  • 这是错误的代理类型。这使您的应用程序成为代理,而不是让您的应用程序使用代理。 (7认同)