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

时间:2015-06-05 10:44:48

标签: .net openssl x509certificate sslstream

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

我正在使用SslStream对象通过传递服务器IPSslStream.AuthenticateAsClientX509CertificateCollection来调用SslProtocols.Ssl3方法来验证Tcp客户端。

我收到以下错误:

  

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

7 个答案:

答案 0 :(得分:115)

我建议不要将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默认使用正确的协议,不需要任何解决方案。

答案 1 :(得分:15)

如果您想使用旧版本的.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);
    }

答案 2 :(得分:13)

添加以下代码帮助我克服了这个问题。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;

答案 3 :(得分: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;
}

答案 4 :(得分:1)

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

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

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

答案 5 :(得分:1)

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);
            }

这对我有用

答案 6 :(得分:0)

当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 Tool,如下所示。

enter image description here

相关问题