c# - WebClient 忽略 ServicePointManager.ServerCertificateValidationCallback?

标签 c# asp.net-mvc ssl webclient

我使用 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.asax Application_Start 方法中:

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

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

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

最佳答案

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

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

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

这个结果是我从这个网站上得到的 协议(protocol)

TLS 1.2 Yes TLS 1.1 Yes TLS 1.0 No SSL 3 No SSL 2 No

关于c# - WebClient 忽略 ServicePointManager.ServerCertificateValidationCallback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27225263/

相关文章:

ssl - 使用 ESP8266 的 SSL 连接失败

c# - 自 TFS .NET 4.5 升级以来,以 .NET 4.0 为目标会产生无效的 Win XP 应用程序

c# - WiX 3.5.2030.0 CreateDatabase 使用 DTF 外部 UI 处理程序失败并显示 1603,在没有外部 UI 处理程序的情况下工作

c# - 使用 CSharpCodeProvider 时出现运行时错误程序集 'System.Runtime'

jquery - ASP.NET MVC 使用 JQuery 将页面内容加载到 div 中

c# - 如何在我的在线站点中引入在线数据库

vb.net - npgsql 连接等待 ssl

c# - XDocument 文本节点换行

c# - 从 PHP 到 ASP.net/C#?

php - 如何向 PHP 添加证书颁发机构,以便 file() 函数信任由它签名的证书?