简体   繁体   English

.NET 4.5.1中的WCF客户端:如何在使用WebRequest时启用TLS 1.2?

[英]WCF Client in .NET 4.5.1: How to enable TLS 1.2 when WebRequest is used?

Our .net WCF Client, the WebRequest call, compiled to a windows EXE, running on Win2012-R2, refuses to connect to a web server that surfaces ONLY TLS 1.2 我们的.net WCF客户端,WebRequest调用,编译到运行在Win2012-R2上的Windows EXE,拒绝连接到仅面向TLS 1.2的Web服务器

We know that Win2012 and .NET 4.5x support TLS 1.2 我们知道Win2012和.NET 4.5x支持TLS 1.2

We have no problems when the server surfaces TLS 1.0 and up. 当服务器表面TLS 1.0及更高版本时,我们没有问题。 The problem is only seen when the server we connect to has DISABLED TLS 1.0, 1.1 and SSL2 and SSL3. 只有当我们连接的服务器具有DISABLED TLS 1.0,1.1和SSL2以及SSL3时才会出现此问题。 The Server ONLY surfaces TLS 1.2. Server ONLY表面TLS 1.2。 Chrome and firefox (on Win 7 and higher) connect fine to the server (no warnings or SSL issues of any kind). Chrome和Firefox(在Win 7及更高版本上)可以很好地连接到服务器(没有任何警告或SSL问题)。

The server certificate is %100 OK. 服务器证书是%100 OK。

The problem is that WebRequest fails to connect in this situation. 问题是WebRequest在这种情况下无法连接。

What do we need to set in code so that our use of WebRequest will connect to systems that may run TLS 1.2, 1.1, 1.0, and/or SSL v3? 我们需要在代码中设置什么,以便我们使用WebRequest将连接到可能运行TLS 1.2,1.1,1.0和/或SSL v3的系统?

您应该使用.NET 4.5或更高版本,并在您的代码中添加以下行:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

While not easy to figure out, the needed property is: 虽然不容易理解,但所需的属性是:

System.Net.ServicePointManager.SecurityProtocol System.Net.ServicePointManager.SecurityProtocol

This can be used to disable and enable TLS levels in the WCF environment. 这可用于在WCF环境中禁用和启用TLS级别。

Further, you can see what WCF is currently set to using: 此外,您可以看到WCF当前使用的内容:

Console.WriteLine(System.Net.ServicePointManager.SecurityProtocol.ToString());

With thanks to: How do I disable SSL fallback and use only TLS for outbound connections in .NET? 感谢: 如何在.NET中禁用SSL回退并仅使用TLS进行出站连接? (Poodle mitigation) (贵宾犬缓解)

What is important, you should start with .Net Framework v4.5 at least. 重要的是,您应该至少从.Net Framework v4.5开始。 Older versions do not support TSL 1.2 . 旧版本支持TSL 1.2 Later on, while authenticating to the server explicitly use this protocol: 稍后,在对服务器进行身份验证时,请明确使用此协议:

    sslStream.AuthenticateAsClient(this._configuration.Host, null, SslProtocols.Tls12, true);

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

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