.NET应用程序无法检测到代理 - 当代理设置为自动发现时

3
摘要:我们的.NET WPF应用程序似乎无法检测到代理,如果代理设置为自动发现。这意味着当我们尝试使用Azure AD进行身份验证并尝试下载用于登录应用程序的令牌时,我们的应用程序将在此后失败。 然而,如果我们在Internet选项中手动定义代理设置,则该应用程序可以正常加载。
即,当下面设置为代理服务器IP时。

enter image description here

另一个有趣的观点是,当代理设置为自动检测时,如果我们通过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>
1个回答

2
你想要检测你网络的代理,然后强制所有外部网络调用通过它进行。请从app.config文件中完全删除代理详细信息,包括整个元素,在部分中删除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属性。


优秀的解决方案,Daniel。谢谢! - Dalibor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接