繁体   English   中英

无法从 .NET C# 创建 SSL/TLS 安全通道

[英]Could not create SSL/TLS secure channel from .NET C#

我正在尝试在我的服务器 windows 2008 R2 上调用 API,但我不断收到此错误:无法创建 SSL/TLS 安全通道

我使用 LocalSystem 帐户将应用程序作为 Windows 服务启动。

我从 Stack Overflow 的一些问题中尝试了很多东西来开始它,但这些都不起作用:

  • 使用 AppContext 开关

  • 没有 AppContext 开关

  • 只有 SSL 安全协议上的协议 1.2

  • 没有更改 SecurityProcotol

  • ExpectContinue = 100

     //Config const string DontEnableSystemDefaultTlsVersions = @"Switch.System.Net.DontEnableSystemDefaultTlsVersions"; const string DontEnableSchUseStrongCryptoName = @"Switch.System.Net.DontEnableSchUseStrongCrypto"; const string DisableUsingServicePointManagerSecurityProtocols = @"Switch.System.ServiceModel.DisableUsingServicePointManagerSecurityProtocols"; const string DontEnableSystemDefaultTlsVersionsServiceModel = @"Switch.System.ServiceModel.DontEnableSystemDefaultTlsVersions"; AppContext.SetSwitch(DontEnableSchUseStrongCryptoName, false); AppContext.SetSwitch(DontEnableSystemDefaultTlsVersions, true); AppContext.SetSwitch(DisableUsingServicePointManagerSecurityProtocols, false); AppContext.SetSwitch(DontEnableSystemDefaultTlsVersionsServiceModel, true); ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; //API config and Call HttpWebRequest request = (HttpWebRequest)WebRequest.Create(message.Url); request.Credentials = CredentialCache.DefaultCredentials; request.Method = message.Method; System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); Byte[] byteArray = encoding.GetBytes(message.Content); request.ContentLength = byteArray.Length; request.ContentType = message.ContentType; request.UserAgent = message.UserAgent; request.Accept = message.Accept; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); }

API 需要 TLS 1.2+ 才能被调用。

我已经测试了 Inte.net Explorer 看我们是否可以调用 TLS 1.2,它似乎可以调用,我使用了这个https://browserleaks.com/ssl : BrowserLeak

我试图从 Postman 在线(使用 Chrome)和服务器上的 Postman 应用程序调用 API,两者都在毫无问题地调用 API。 与 SoapUI 一样,它可以工作。

它适用于我的计算机(Windows 10),但不适用于服务器,具有相同的 C# 代码。

我试过换框架,4.0、4.5、4.6、4.8,好像没什么变化。

我有 SSL 握手,但没有太大帮助,它说握手失败 (02 28):

    System.Net.Sockets Verbose: 0 : [51080] Exiting Socket#40535505::Send()     -> Int32#174
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.5541547Z
System.Net.Sockets Verbose: 0 : [51080] Entering Socket#40535505::Receive()
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.5541547Z
System.Net.Sockets Verbose: 0 : [51080] Data from Socket#40535505::Receive
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6151608Z
System.Net.Sockets Verbose: 0 : [51080] 00000000 : 15 03 03 00 02                                  : .....
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6151608Z
System.Net.Sockets Verbose: 0 : [51080] Exiting Socket#40535505::Receive()  -> Int32#5
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6151608Z
System.Net.Sockets Verbose: 0 : [51080] Entering Socket#40535505::Receive()
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6161609Z
System.Net.Sockets Verbose: 0 : [51080] Data from Socket#40535505::Receive
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6161609Z
System.Net.Sockets Verbose: 0 : [51080] 00000005 : 02 28                                           : .(
    ProcessId=46588
    ThreadId=6
    DateTime=2021-11-24T09:05:57.6161609Z

密码套件似乎不错,我在套件开头有 API 所需的前 3 个密码套件。

API 需要 ISRG 根 X1 证书,该证书位于受信任的根证书颁发机构存储中。

我使用 windows 的工具修改注册表https://support.microsoft.com/en-us/topic/update-to-enable-tls-1-1-and-tls-1-2-as-default- secure-protocols-in-winhttp-in-windows-c4bd73d2-31d7-761e-0178-11268bb10392启用了 TLS 1.2,在我启动该工具之前它没有在 browserleaks 中激活。

此外 SSL 2.0 和 SSL3.0 被禁用,TLS1.0、TLS1.1 和 TLS1.2 被激活:安全通道注册表

我似乎找不到解决方案来让它工作。 我怎样才能找到问题的线索?

我们发现了问题,API 所需的密码套件与 windows 服务器 2008 R2 不兼容,我们验证了密码套件

https://www.ssllabs.com/ssltest/与 api 的 url

然后将其与位于此处的服务器上的密码套件进行比较:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers

他们都不匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM