[英]How to implement Security Protocols TLS 1.2 in .Net 3.5 framework
隨着Paypal更新他們的響應,我需要在.NET 3.5框架上的現有應用程序中將安全協議TLS更新到v1.2。 在現有代碼中更新此更改所需的更改,我無法將應用程序更新到更新的框架。
我正在使用VS 2008與.net 3.5.30729.4926。 我所要做的就是:
添加進口:
Imports System.Security.Authentication
Imports System.Net
將其添加到我的代碼(C#):
public const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
public const SecurityProtocolType Tls12 = (SecurityProtocolType)_Tls12;
ServicePointManager.SecurityProtocol = Tls12;
VB.net版本:
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()
只需在vb .net 3.5版本中添加您的代碼:
ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
那么你的代碼變成:
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"
.............
希望這個幫助
如果您使用的是.NET 3.5.1,則可以選擇應用匯總修補程序並應用注冊表編輯以告知.NET使用系統默認值。 更多細節在這里
如果您未能使用.NET 4.5 for TLS 1.2和1.1支持,並且至少需要在Windows Server 2008 R2上使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.