身份验证失败,因为远程方已关闭传输流

问题描述 投票:0回答:10

我正在开发一个 TCP 客户端来连接 OpenSSL 服务器和证书身份验证。我使用服务器团队共享的 .crt 和 .key 文件。这些证书由 OpenSSL 命令生成。

我正在使用

SslStream
对象通过传递服务器SslStream.AuthenticateAsClient
IP
SslProtocols.Ssl3
调用
X509CertificateCollection
方法来验证Tcp客户端。

我收到以下错误:

认证失败,因为对方关闭了传输流

.net openssl x509certificate sslstream
10个回答
176
投票

我建议不要将 SecurityProtocol 限制为 TLS 1.1.

推荐的方案是使用

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

另一个选项是添加以下注册表项:

Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319 
Value: SchUseStrongCrypto 

值得注意的是,.NET 4.6 将默认使用正确的协议,并且不需要任何一种解决方案。


18
投票

如果你想使用旧版本的.net,创建你自己的标志并投射它。

    //
    // Summary:
    //     Specifies the security protocols that are supported by the Schannel security
    //     package.
    [Flags]
    private enum MySecurityProtocolType
    {
        //
        // Summary:
        //     Specifies the Secure Socket Layer (SSL) 3.0 security protocol.
        Ssl3 = 48,
        //
        // Summary:
        //     Specifies the Transport Layer Security (TLS) 1.0 security protocol.
        Tls = 192,
        //
        // Summary:
        //     Specifies the Transport Layer Security (TLS) 1.1 security protocol.
        Tls11 = 768,
        //
        // Summary:
        //     Specifies the Transport Layer Security (TLS) 1.2 security protocol.
        Tls12 = 3072
    }
    public Session()
    {
        System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(MySecurityProtocolType.Tls12 | MySecurityProtocolType.Tls11 | MySecurityProtocolType.Tls);
    }

13
投票

添加以下代码帮助我解决了这个问题。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;

11
投票
using (var client = new HttpClient(handler))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiEndPoint)).ConfigureAwait(false);
                await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            }

这对我有用


5
投票

我在使用 ChargifyNET.dll 与 Chargify API 通信时遇到了同样的错误消息。将

chargify.ProtocolType = SecurityProtocolType.Tls12;
添加到配置中解决了我的问题。

这里是完整的代码片段:

public ChargifyConnect GetChargifyConnect()
{
    var chargify = new ChargifyConnect();
    chargify.apiKey = ConfigurationManager.AppSettings["Chargify.apiKey"];
    chargify.Password = ConfigurationManager.AppSettings["Chargify.apiPassword"];
    chargify.URL = ConfigurationManager.AppSettings["Chargify.url"];

    // Without this an error will be thrown.
    chargify.ProtocolType = SecurityProtocolType.Tls12;

    return chargify;
}

3
投票

对于 VB.NET,您可以在 Web 请求之前放置以下内容:

Const _Tls12 As SslProtocols = DirectCast(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = DirectCast(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12

这解决了我在 .NET 3.5 上的安全问题。


1
投票

当 Web 请求端点切换到另一台仅接受 TLS1.2 请求的服务器时,这发生在我身上。尝试了很多在 Stackoverflow 上发现的尝试,比如

  1. 注册表键,
  2. 添加:
    System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;到 Global.ASX OnStart,
  3. 添加到 Web.config.
  4. 更新 .Net 框架至 4.7.2 仍然有同样的异常。

收到的异常没有反映我面临的实际问题,也没有找到服务运营商的帮助。

为了解决这个问题,我必须添加一个新的密码套件TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 我使用了 here 中的 IIS Crypto 2.0 工具,如下所示。


0
投票

我在 VS 2019 中遇到了以下问题:

  • 收听无效
  • 扩展 -> 管理扩展 -> 无法检索扩展信息。

我已经添加了一个新的密码套件 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384,正如 FunMatters 所解释的那样。这也解决了我的问题。


0
投票

在 .NET Framework 4.8 我在发送请求之前添加了以下代码。

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

0
投票

我遇到了这个问题并修复了它,但是在这篇文章发布时在这里找到的解决方案(即 TSL 解决方案)都没有对我有用,我尝试了各种方法。 我花了几个小时尝试,搜索,尝试,一切似乎都集中在 TSL 问题上,这些都没有解决我的问题。

我必须做的是使用 Visual Studio Installer 并修复我的 VS 安装。之后,马上修好了

© www.soinside.com 2019 - 2024. All rights reserved.