As Paypal updated their response, I need to update security protocols TLS to v1.2 in my existing application which is on .NET 3.5 framework. What changes required to update this in existing code, I cannot update the application to newer framework.
3 Answers
I'm using VS 2008 with .net 3.5.30729.4926. All I had to do was:
Add imports:
Imports System.Security.Authentication
Imports System.Net
Add this to my code (C#):
public const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
public const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
ServicePointManager.SecurityProtocol = Tls12;
VB.net version:
Const _Tls12 As SslProtocols = DirectCast(&HC00, SslProtocols)
Const Tls12 As SecurityProtocolType = DirectCast(_Tls12, SecurityProtocolType)
ServicePointManager.SecurityProtocol = Tls12
Dim wbrq As HttpWebRequest
Dim wbrs As HttpWebResponse
Dim sw As StreamWriter
Dim sr As StreamReader
Dim strResult As String
'Create a new HttpWebRequest object.
wbrq = WebRequest.Create(strURL)
wbrq.Method = "POST"
wbrq.ContentLength = DataString.Length
wbrq.ContentType = "application/x-www-form-urlencoded"
'upload data
sw = New StreamWriter(wbrq.GetRequestStream)
sw.Write(DataString)
sw.Close()
'get response
wbrs = wbrq.GetResponse
sr = New StreamReader(wbrs.GetResponseStream)
strResult = sr.ReadToEnd.Trim
sr.Close()
-
1@Cullub Thanks. I suspect MS might retrofit the constant in older versions of .net.– D_BesterAug 4, 2017 at 15:50
-
This is better than the other answer at the moment - it doesn't depend on a broken link ;-)– CullubAug 4, 2017 at 15:52
-
-
1@Anna The SecurityProtocol is set immediately before making a HttpWebRequest. See my edited post above.– D_BesterAug 16, 2017 at 13:24
-
1
just adding adding Your code in vb .net 3.5 version :
ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
then Your code become :
ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
Dim wbrq As HttpWebRequest
Dim wbrs As HttpWebResponse
Dim sw As StreamWriter
Dim sr As StreamReader
Dim strResult As String
'Create a new HttpWebRequest object.
wbrq = WebRequest.Create(strURL)
wbrq.Method = "POST"
wbrq.ContentLength = DataString.Length
wbrq.ContentType = "application/x-www-form-urlencoded"
.............
hope this help
If you are on NET 3.5.1 you have an option of applying a rollup hotfix and apply a registry edit to tell .NET to use the system default. More details here
Failing that you need to be using .NET 4.5 for TLS 1.2 & 1.1 support and on Windows Server 2008 R2 at a minimum.