.net 身份验证失败,因为远程方已关闭传输流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30664566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Authentication failed because remote party has closed the transport stream
提问by Odelu
I am developing a TCP client to connect OpenSSL server with the certificate authentication. I have using .crt and .key files shared by server team. These certificates are generated by OpenSSL commands.
我正在开发一个 TCP 客户端来连接 OpenSSL 服务器和证书身份验证。我使用了服务器团队共享的 .crt 和 .key 文件。这些证书由 OpenSSL 命令生成。
I am using SslStreamobject to authenticate the Tcp client by calling SslStream.AuthenticateAsClientmethod by passing server IP, SslProtocols.Ssl3and X509CertificateCollection.
我使用的SslStream对象调用来验证TCP客户端SslStream.AuthenticateAsClient通过将服务器的方法IP,SslProtocols.Ssl3和X509CertificateCollection。
I am getting the following error:
我收到以下错误:
Authentication failed because the remote party has closed the transport stream
身份验证失败,因为远程方已关闭传输流
回答by GuiSim
I would advise against restricting the SecurityProtocol to TLS 1.1.
我建议不要将 SecurityProtocol 限制为 TLS 1.1。
The recommended solution is to use
推荐的解决方案是使用
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls
Another option is add the following Registry key:
另一种选择是添加以下注册表项:
Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319
Value: SchUseStrongCrypto
It is worth noting that .NET 4.6 will use the correct protocol by default and does not require either solution.
值得注意的是,.NET 4.6 将默认使用正确的协议,不需要任何一种解决方案。
回答by Iguanaware
If you want to use an older version of .net, create your own flag and cast it.
如果您想使用旧版本的 .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);
}
回答by muruge
Adding the below code helped me overcome the issue.
添加以下代码帮助我克服了这个问题。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;
回答by Sanket Sonavane
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);
}
This worked for me
这对我有用
回答by Tod Birdsall
I ran into the same error message while using the ChargifyNET.dll to communicate with the Chargify API. Adding chargify.ProtocolType = SecurityProtocolType.Tls12;to the configuration solved the problem for me.
我在使用 ChargifyNET.dll 与 Chargify API 通信时遇到了相同的错误消息。添加chargify.ProtocolType = SecurityProtocolType.Tls12;到配置中解决了我的问题。
Here is the complete code snippet:
这是完整的代码片段:
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;
}
回答by user1477388
For VB.NET, you can place the following before your web request:
对于 VB.NET,您可以在 Web 请求之前放置以下内容:
Const _Tls12 As SslProtocols = DirectCast(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = DirectCast(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12
This solved my security issue on .NET 3.5.
这解决了我在 .NET 3.5 上的安全问题。

