【问题标题】:.NET application not detecting proxy - when proxy is set to auto discover.NET 应用程序未检测到代理 - 当代理设置为自动发现时
【发布时间】:2018-01-29 23:40:52
【问题描述】:

总结:如果代理设置为自动发现,我们的 .NET WPF 应用程序似乎不会检测到代理。这意味着我们的应用程序在尝试使用 Azure AD 进行身份验证以及我们尝试下载用于登录应用程序的令牌时失败。

但是,如果我们在 Internet 选项中手动定义代理设置,则应用程序可以正常加载。

即当下面设置为代理服务器IP。

另一个有趣的点是,当代理设置为自动检测我们是否通过 Internet Explorer 或 chrome 联系我们的服务时,我们可以看到 WSDL。

我们怀疑问题可能出在我们系统的 Microsoft azure 身份验证部分绕过代理。在这个特定的网络设置中,任何绕过代理的东西都无法通过火灾,因此,应用程序失败并出现错误“未知错误:未知错误”。

我们尝试过的主要内容包括: • 这在 app.config 的 system.net 部分中:

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

• 这在 app.config 的绑定部分中(注意 bypassProxyOnLocal=”true”、useDefaultWebProxy=”true” 和 proxyCredentialType=”Windows”):

<wsHttpBinding>
        <binding name="InternalWsHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="true" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="None" proxyCredentialType="Windows" realm="" />
          </security>
        </binding>
      </wsHttpBinding>

【问题讨论】:

    标签: .net wpf


    【解决方案1】:

    您想检测网络的代理,然后强制所有外部网络调用都通过它。

    从 app.config 文件中完全删除代理详细信息,包括整个 &lt;system.net&gt; 元素以及 &lt;binding&gt; 部分中的 bypassProxyOnLocal 属性、useDefaultWebProxy 属性和 proxyCredentialType 属性。

    然后,在您的代码中,在您进行任何网络调用之前,检测网络的代理并将其用作 DefaultWebProxy:

    var testAddress = new Uri("http://google.com"); //can be any external URL that goes through the network's proxy
    var proxy = WebRequest.DefaultWebProxy.GetProxy(testAddress); //returns the testAddress if it can't find a proxy
    if (proxy != testAddress)
    {
        //Found a proxy!
        var webProxy = new WebProxy(proxy, BypassOnLocal: true) //BypassOnLocal is your choice!
        {
            UseDefaultCredentials = true
        };
        WebRequest.DefaultWebProxy = webProxy;
    }
    

    您的所有网络调用现在都应该通过自动检测到的代理。如果您正在与本地网络中的服务器通信,请根据您是否要对本地网络中的地址使用代理来设置 BypassOnLocal 属性。

    【讨论】:

    • 优秀的解决方案丹尼尔。谢谢!
    猜你喜欢
    • 2010-10-25
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2010-11-12
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    相关资源
    最近更新 更多