【问题标题】:C# Console Application HttpClient throws "System.Net.Http.WinHttpException: A security error occurred"C# 控制台应用程序 HttpClient 抛出“System.Net.Http.WinHttpException:发生安全错误”
【发布时间】:2020-04-04 12:53:58
【问题描述】:

我有一个在 Windows Server 2012 R2 中运行的带有 .Net framework 4.6 的 C# 控制台应用程序。我正在使用“IIS Crypto 3.1”来更改安全协议中的一些设置,但是在使用“IIS Crypto”中称为“最佳实践”的功能(自动设置客户端和服务器协议)之后,我无法让我的应用程序调用任何再次通过 HttpClient 的 URL(它曾经在此之前工作)。这是我的代码:

 using (HttpClient hc = new HttpClient())
  {
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 
      | SecurityProtocolType.Tls;

       ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, 
       errors) => { return true; };
       var response = await hc.GetAsync(download_path);
       content = response.Content;
   }

这是我在点击“hc.GetAsync()”后得到的异常:

System.AggregateException: One or more errors occurred. ---> System.AggregateExc
eption: One or more errors occurred. ---> System.Net.Http.HttpRequestException:
An error occurred while sending the request. ---> System.Net.Http.WinHttpExcepti
on: A security error occurred

这是我的安全协议的当前设置:

很遗憾,我不知道如何恢复我在此处更改的设置(此应用更改了一些注册表项)。

更新:

所以问题似乎只在调用 Telegram Bot API 的特定 URL 时出现(类似于 https://api.telegram.org/.....)。对于其他 URL(据我所知),这个全新的设置仍然有效。我必须说,之前的设置完全适用于这个相同的 URL。

【问题讨论】:

  • download_path ..... 这是 https 吗?还是只是 http?
  • @JonathanAlfaro 是的,它是 Telegram Bot API 的 https 地址。我实际上是在连续试用 7 小时后才发现问题的。我会更新帖子并相应地回答。简而言之,问题在于我在使用“IIS Crypto”后更改的“密码套件”顺序。将更改恢复为默认顺序将对问题进行排序。

标签: c# .net console-application dotnet-httpclient windows-server-2012-r2


【解决方案1】:

经过7个小时的不断挣扎,我终于找到了问题所在。显然,“IIS Crypto”改变了 Windows Server 2012 R2 中“Cipher Suites”的顺序。我只是启用/禁用协议本身,而不是他们的密码命令。将密码套件的顺序更改为其默认顺序后(请注意,此顺序是特定于操作系统的。因此它在 Windows Server 2008、2012、2016 中有所不同......我在“IIS Crypto”网页中找到了默认值。) 似乎 Telegram 服务器对密码套件的顺序很敏感,并且在更改时不响应(我不知道这里的确切技术)。这就是它在 Windows Server 2012 R2 中的样子:

此外,我在运行 Windows 10 的笔记本电脑中检查了 IIS Crypto,在使用了相同的功能(即“最佳实践”)之后,我没有遇到任何问题。所以这个问题似乎与 Windows Server 2012 R2 有某种关系

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    相关资源
    最近更新 更多