【问题标题】:How to implement Security Protocols TLS 1.2 in .Net 3.5 framework如何在 .Net 3.5 框架中实现安全协议 TLS 1.2
【发布时间】:2017-07-18 00:22:17
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net .net paypal tls1.2


    【解决方案1】:

    我正在使用 .net 3.5.30729.4926 的 VS 2008。我所要做的就是:

    添加导入:

    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()  
    

    【讨论】:

    • @Cullub 谢谢。我怀疑 MS 可能会在旧版本的 .net 中改进常量。
    • 目前这比其他答案更好 - 它不依赖于断开的链接;-)
    • 这个放在代码的什么地方?在课堂上?还是global.asax?等等?
    • @Anna SecurityProtocol 在发出 HttpWebRequest 之前立即设置。请参阅上面我编辑的帖子。
    • @Anna 没有修补程序或注册表编辑。就是上面的代码。 YMMV
    【解决方案2】:

    只需在 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"
    .............
    

    希望有帮助

    【讨论】:

      【解决方案3】:

      如果您使用的是 NET 3.5.1,您可以选择应用汇总修补程序并应用注册表编辑来告诉 .NET 使用系统默认值。 More details here

      您需要使用 .NET 4.5 来支持 TLS 1.2 和 1.1 并至少在 Windows Server 2008 R2 上使用。

      【讨论】:

      • 您的链接已损坏。
      猜你喜欢
      • 2017-10-23
      • 2017-08-31
      • 2017-03-08
      • 2021-03-26
      • 2018-12-26
      • 2019-06-18
      • 1970-01-01
      • 2016-06-16
      • 2011-01-12
      相关资源
      最近更新 更多