.NET40兼容,默认框架越新集成的TLS协议版本就越高,最新最安全的是TLS1.3版本。
[SecurityCritical]
[SecuritySafeCritical]
private static void InitialSecurityProtocol()
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls
| (SecurityProtocolType)0x300 // Tls11
| (SecurityProtocolType)0xC00 // Tls12
| (SecurityProtocolType)0x3000; // Tls13
}
catch (Exception)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls
| (SecurityProtocolType)0x300 // Tls11
| (SecurityProtocolType)0xC00; // Tls12
}
catch (Exception)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls
| (SecurityProtocolType)0x300; // Tls11
}
catch (Exception)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
| SecurityProtocolType.Tls;
}
catch (Exception) { }
}
}
}
}