System.Net.SecurityProtocolType.Tls12 definition not found
Solution 1
SecurityProtocolType.Tls11 and SecurityProtocolType.Tls12 enum values are missing on Framework 4.0 only.
SecurityProtocolType numeric values:
SystemDefault (0)
Ssl3 (48 - 0x30)
Tls (192 - 0xC0)
Tls11 (768 - 0x300) missing on Framework 4.0
Tls12 (3072 - 0xC00) missing on Framework 4.0
On Framework 4.0, if want to allow TLS 1.0, 1.1 and 1.2, just replace:
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
by:
(SecurityProtocolType)(0xc0 | 0x300 | 0xc00)
Solution 2
Are you on .net 4.0? You should be at least 4.5 to use it. You can try to update your web target framework version: TLS 1.2 in .NET Framework 4.0
Solution 3
TLS and How to avoid connection errors.
- .NET 4.6 and above. You don’t need to do any additional work to support TLS 1.2, it’s supported by default.
- .NET 4.5. TLS 1.2 is supported but it’s not a default protocol. You need to opt-in to use it. The following code will make TLS 1.2 default, make sure to execute it before making a connection to secured resource:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- .NET 4.0. TLS 1.2 is not supported, but if you have .NET 4.5 (or above) installed on the system then you still can opt in for TLS 1.2 even if your application framework doesn’t support it. The only problem is that SecurityProtocolType in .NET 4.0 doesn’t have an entry for TLS1.2, so we’d have to use a numerical representation of this enum value:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
- .NET 3.5 or below. TLS 1.2 is not supported (*) and there is no workaround. Upgrade your application to more recent version of the framework.
Personally on my .Net 4.0 Framework with some asp classic files I used:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
https://blogs.perficient.com/2016/04/28/tsl-1-2-and-net-support/
Solution 4
Website is already on .Net 4.5, Later updating the Compilation > TargetFramework manually from 4.0 to 4.5 fixed the issue for me.
Here's is the updated configuration
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
HockChai Lim
Updated on July 09, 2022Comments
-
HockChai Lim almost 2 years
I'm trying to add the following line of code to the Global.asax file in a website project.
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
The vs2012 IntelliSense is showing that Tls12 definition exist. But the build is saying that the definition does not exist (See screen shot).
I've tried adding System.Net.dll to the bin folder of the project, but the build still failed. Any idea how I might be able to resolve this?
-
HockChai Lim over 6 yearsit is a website project. Is there a way to specify such? I do see this in the project's web.config file: <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> </startup>
-
HockChai Lim over 6 yearsIt seems to work after I re-select the target of .NET4.5. Detail:: Right Click the project => Property Pages => Under Build, re-select Target Framework value =>.NET Framework 4.5 (Note: That was the current value). Once I did that, the build works. Weird...
-
Teoman shipahi over 6 yearsMaybe it was stuck on prev version in VS cache. Yeah, that sounds weird.
-
HockChai Lim over 6 yearsAfter I did above, I noticed the following being added to the web.config: <system.web> <compilation targetFramework="4.5"> <assemblies> <add assembly="System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> </assemblies> </compilation> <pages controlRenderingCompatibilityVersion="4.0"/> </system.web>
-
Teoman shipahi over 6 yearscompilation targetFramework="4.5" should have done the trick.
-
Louis Somers over 5 yearsThere also used to be a
SecurityProtocolType.SystemDefault
(0x00) that disappeared again after some update. -
cmartin over 4 yearsThanks, this worked in my .NET 4.0, Silverlight 5 project.
-
Mike W over 4 yearsthe VB version of this is CType((&HC0 Or &H300 Or &HC00), System.Net.SecurityProtocolType)
-
Pranesh Janarthanan over 4 yearsHi, I am facing this exception error
Authentication failed because the remote party has closed the transport stream.
. during a HTTP request. Client runs under DotNet 4.0, I used the codeServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
before sending the request. Still i am getting exception error. Do you have any clue fix it? -
Landscaper3345 over 4 yearsHave you tried: ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
-
Pranesh Janarthanan over 4 yearsYes, during bebug. First HTTP Request fails with this error, and further consecutive request is pass. Why its failed in first?
-
Asaad Mamoun over 3 yearsThank you very much for this (SecurityProtocolType)(0xc0 | 0x300 | 0xc00) it saved my day :)
-
dcarl661 about 3 yearsYes thank you very much for this it saved my day as well.
-
pghcpa about 2 yearsVb.net: System.Net.ServicePointManager.SecurityProtocol = CType((&HC0 Or &H300 Or &HC00), SecurityProtocolType)
-
Ali.DM about 2 yearsAdding (SecurityProtocolType)(0xc0 | 0x300 | 0xc00) worked for me. But this is so ridiculous. Why should these protocols be removed and then added manually to work? By the way, I'm so grateful @figolu