在所有连接上强制 ServicePointManager.SecurityProtocol 到 TLS 1.2

Force ServicePointManager.SecurityProtocol to TLS 1.2 on all connections

我有一个发送传出请求的 WCF 服务。目前它正在使用 SSL 3.0TLS 1.0.

我发送请求的服务现在只接受TLS 1.2

我可以在请求之前(以及每个请求)设置 SecurityProtocolType,但我希望它对所有传出请求使用 TLS 1.2,而不必为每个请求指定它。

此代码为请求正确设置:

<OperationContract(), WebGet(UriTemplate:="...")>
Public Function SomeService()

    System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType) 3072; // 3072 is TLS 1.2

    // Do request

End Function

但我看不到如何将 WCF 设置为对所有请求使用 TLS 1.2。我尝试将上述语句放入 Application_StartGlobal.asax 中的 Application_BeginRequest,但是在执行请求时,SecurityProtocol 又回到了 SSL3/TLS1.0

如果您有权访问注册表,则可以应用以下密钥: registry key

这会在传输层的 Windows 级别强制执行 TLS 1.2,因此您无需更改任何代码。

这些是上述文件中更改的密钥:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

在 Startup.Auth.cs 文件中添加这一行。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

这对我们有用。