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

标签 .net wpf

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

但是,如果我们在 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 的绑定(bind)部分(注意 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>

最佳答案

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

从 app.config 文件中完全删除代理详细信息,包括整个 <system.net>元素,在 <binding> 中节 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 属性。

关于.NET 应用程序未检测到代理 - 当代理设置为自动发现时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511796/

相关文章:

c# - 在长时间未使用后保持 .NET alt-tab 替换响应的策略?

wpf - 填充并绑定(bind)两个组合框 WPF Caliburn.micro

Designer中的WPF功能区选项卡 View

c# - 如何使用 C#2010 将图表图像复制到剪贴板?

c# - WCF 服务作为 MVC 应用程序的一部分

wpf - 让 WPF Tabcontrol 高度假设最大项目的高度?

wpf - 更改 TabControl 中选定选项卡项的文本颜色?

.net - T[] 和 List<T> 在内存方面的内部差异是什么?

javascript - 单击时使用 jquery 从数据网格行获取值

c# - 检查在上下文菜单条中单击了哪个子菜单项