C# 如何在.NET3.5框架中实现安全协议TLS1.2

C# 如何在.NET3.5框架中实现安全协议TLS1.2,c#,asp.net,.net,paypal,tls1.2,C#,Asp.net,.net,Paypal,Tls1.2,随着Paypal更新了他们的响应,我需要将现有应用程序中的安全协议TLS更新为v1.2,该应用程序位于.NET3.5框架上。 如果在现有代码中需要进行哪些更改才能更新此内容,则我无法将应用程序更新到更新的框架 如果您在NET 3.5.1上,您可以选择应用汇总热修复程序并应用注册表编辑,以告知.NET使用系统默认设置。 否则,您至少需要在Windows Server 2008 R2上使用.NET 4.5来支持TLS 1.2和1.1。我将VS 2008与.NET 3.5.30729.4926一起使

随着Paypal更新了他们的响应,我需要将现有应用程序中的安全协议TLS更新为v1.2,该应用程序位于.NET3.5框架上。
如果在现有代码中需要进行哪些更改才能更新此内容,则我无法将应用程序更新到更新的框架

如果您在NET 3.5.1上,您可以选择应用汇总热修复程序并应用注册表编辑,以告知.NET使用系统默认设置。


否则,您至少需要在Windows Server 2008 R2上使用.NET 4.5来支持TLS 1.2和1.1。我将VS 2008与.NET 3.5.30729.4926一起使用。我所要做的就是:

添加导入:

Imports System.Security.Authentication
Imports System.Net
将此添加到我的代码(C#)中:

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"
.............

希望此帮助

您的链接已断开。@culub谢谢。我怀疑MS可能会在较旧版本的.net中改进常量。这比目前的另一个答案要好——它不依赖于断开的链接;-)这在代码中的什么地方?在课堂上?还是global.asax?等等?@Anna安全协议在发出HttpWebRequest之前立即设置。请参阅上面我编辑过的文章。@Anna没有修补程序或注册表编辑。只是上面的代码。YMMV
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"
.............