【问题标题】:WebClient ignores ServicePointManager.ServerCertificateValidationCallback?WebClient 忽略 ServicePointManager.ServerCertificateValidationCallback?
【发布时间】:2015-01-29 06:34:09
【问题描述】:

我使用 WebClient 从我的 MVC5 项目的外部网站获取数据:

        using (var client = new WebClient())
        {
            var result = client.DownloadString(url);

            return this.Content(result);
        }

但是,请求的 URL 使用 TLS,但我们处于开发阶段,所以他们使用自己的证书,这显然是不合法的。因此,我收到此异常:

[IOException: Authentication failed because the remote party has closed the transport stream.]
   System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) +6587998
   System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) +132
   System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) +59
   System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) +247
   System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) +137
   System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) +119
   System.Net.TlsStream.CallProcessAuthentication(Object state) +38
   System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +166
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +21
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) +64
   System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) +797
   System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) +52
   System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) +19
   System.Net.ConnectStream.WriteHeaders(Boolean async) +142

我已经阅读了很多其他的文章,所以我在我的Global.asaxApplication_Start方法中添加了这些代码:

        System.Net.ServicePointManager.ServerCertificateValidationCallback =
            (s, cert, chain, sslPolicyErrors) =>
            {
                return true;
            };

但是,结果保持不变。我什至尝试在 Callback set 行设置断点,在 return true 行设置 1。在开始时,断点按预期命中。然而,当 WebClient 调用时,return true 行处的断点永远不会被命中。

WebClient 是否使用了另一个回调方法?我该如何解决这个问题?

【问题讨论】:

  • 您可以使用网络浏览器下载该网址吗?如果外部网站使用不受信任的证书,您可以告诉您的网络浏览器信任该证书。您还可以告诉WebClient 信任或忽略该证书,因为 WebClient 就像一个网络浏览器。
  • @damphat 是的。我将 URL 粘贴到浏览器,浏览器警告我证书未签名。我可以选择忽略并继续。但是,对于 WebClient,我无法做到这一点(或者,完全不知道如何成功地做到这一点)。

标签: c# asp.net-mvc ssl webclient


【解决方案1】:

问题是你必须添加这样的东西

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 
                                     | SecurityProtocolType.Tls12;

但这取决于它支持哪些协议。你可以在这里查看 https://www.ssllabs.com

这个结果来自这个网站 协议

TLS 1.2 是 TLS 1.1 是 TLS 1.0 否 SSL 3 否 SSL 2 否

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2020-10-14
    • 2012-12-07
    • 2014-12-10
    • 1970-01-01
    相关资源
    最近更新 更多