我正在开发一个TCP客户端,使用证书认证连接OpenSSL服务器。我使用服务器团队共享的.crt和.key文件,这些证书是通过OpenSSL命令生成的。
我正在使用SslStream
对象调用SslStream.AuthenticateAsClient
方法对Tcp客户端进行身份验证,其中传递了服务器IP地址、SslProtocols.Ssl3
以及X509CertificateCollection
。
我遇到了以下错误:
身份验证失败,因为远程方已关闭传输流
我正在开发一个TCP客户端,使用证书认证连接OpenSSL服务器。我使用服务器团队共享的.crt和.key文件,这些证书是通过OpenSSL命令生成的。
我正在使用SslStream
对象调用SslStream.AuthenticateAsClient
方法对Tcp客户端进行身份验证,其中传递了服务器IP地址、SslProtocols.Ssl3
以及X509CertificateCollection
。
我遇到了以下错误:
身份验证失败,因为远程方已关闭传输流
我建议不要将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默认使用正确的协议,无需任何解决方案。
System.Net.ServicePointManager.SecurityProtocol = ...
这条语句。重要的是要注意,在此之前必须先执行该语句。 - Tonatio //
// 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);
}
ServicePointManager.SecurityProtocol = (SecurityProtocolType) 48
| (SecurityProtocolType) 192
| (SecurityProtocolType) 768
| (SecurityProtocolType) 3072;
这样做可以设置安全协议类型。 - Yan F.加入下面的代码帮助我解决了这个问题。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
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);
}
这对我有用
我在使用 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;
}
对于VB.NET,您可以在Web请求之前放置以下内容:
Const _Tls12 As SslProtocols = DirectCast(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = DirectCast(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12
这解决了我在.NET 3.5上的安全问题。
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
我在VS 2019中遇到了以下问题:
我按照FunMatters的说明添加了一个新的Cipher Suite TLS_DHE_RSA_WITH_AES_256_GCM_SHA384。这也解决了我的问题。
SslProtocols.Ssl3
。也许你应该尝试使用SslProtocols.Tls
。在 .Net 4.5 及以上版本中,你还可以使用Tls11
或Tls12
。请参阅SslProtocols枚举。你可能还有其他问题。 - jww