Default SecurityProtocol in .NET 4.5

前端 未结 17 1733
一生所求
一生所求 2020-11-22 03:24

What is the default security protocol for communicating with servers that support up to TLS 1.2? Will .NET by default, choose the highest security

17条回答
  •  醉梦人生
    2020-11-22 04:09

    The default System.Net.ServicePointManager.SecurityProtocol in both .NET 4.0/4.5 is SecurityProtocolType.Tls|SecurityProtocolType.Ssl3.

    .NET 4.0 supports up to TLS 1.0 while .NET 4.5 supports up to TLS 1.2

    However, an application targeting .NET 4.0 can still support up to TLS 1.2 if .NET 4.5 is installed in the same environment. .NET 4.5 installs on top of .NET 4.0, replacing System.dll.

    I've verified this by observing the correct security protocol set in traffic with fiddler4 and by manually setting the enumerated values in a .NET 4.0 project:

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 |
    (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    

    Reference:

    namespace System.Net
    {
        [System.Flags]
        public enum SecurityProtocolType
        {
           Ssl3 = 48,
           Tls = 192,
           Tls11 = 768,
           Tls12 = 3072,
        }
    }
    

    If you attempt the hack on an environment with ONLY .NET 4.0 installed, you will get the exception:

    Unhandled Exception: System.NotSupportedException: The requested security protocol is not supported. at System.Net.ServicePointManager.set_SecurityProtocol(SecurityProtocolType v alue)

    However, I wouldn't recommend this "hack" since a future patch, etc. may break it.*

    Therefore, I've decided the best route to remove support for SSLv3 is to:

    1. Upgrade all applications to .NET 4.5
    2. Add the following to boostrapping code to override the default and future proof it:

      System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

    *Someone correct me if this hack is wrong, but initial tests I see it works

提交回复
热议问题