如何在ASP.NET 2.0应用程序中为API调用启用TLS 1.2?

How to enable TLS 1.2 for API call in ASP.NET 2.0 application?

我们的ASP.NET 2.0网站通过调用Authorize.Net的API处理信用卡交易。授权已通知我们,在确定的某个日期,我们的客户必须使用TLS 1.2协议进行API调用。

Microsoft似乎指示此10-22-16 KB文章中提供了一种解决方案:https://support.microsoft.com/en-us/help/3154517/support-for-tls-system-default-versions-included -在-.net-framework-2.0-sp2-在Windows上的vista-sp2-和服务器2008-sp2

...we have added the SslProtocolsExtensions enumeration that you can
use as an option for setting TLS v1.2, TLS v1.1, as well as operating
system defaults for the ServicePointManager.SecurityProtocol property
when targeting .NET framework version 2.0 SP2.

请注意,尽管有本文的标题,但上面的引用并未引用Windows Vista SP2或Windows 2008 SP2操作系统,因为这些操作系统不支持TLS v1.1和1.2。

通过执行以下步骤,我已经实现并测试了对知识库文章中指示的解决方案的理解:

  • 在我们的Windows Server 2008 R2 Web服务器上启用了TLS 1.2(并已通过ssllabs.com确认)。
  • 确认实际上已经为.NET Framework 2.0版安装了SP2。
  • 将引用的知识库文章中显示的两个源文件添加到我们的项目中(即SecurityProtocolTypeExtensions.cs和SslProtocolsExtensions.cs)
  • 在API调用上方的项目中输入以下代码行(来自KB文章):System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolTypeExtensions.Tls12;
  • 不幸的是,在运行应用程序时,我在上面第3项所示的代码行中遇到以下错误:

    System.NotSupportedException: The requested security protocol is not
    supported.

    在这一点上,我很沮丧。我特别感谢您对如何继续使用此解决方案的任何见解,但对了解您知道允许从ASP.NET 2.0应用程序进行API调用以利用TLS 1.2的任何其他方法感兴趣。 (升级到.NET框架的最新版本是不得已的方法。)

    在此先感谢您的帮助!


    我们必须使用.NET 2.0应用程序迁移到TLS 1.2,并且我们不想将代码移植到.NET 4.5 / 4.6。经过几天的研究,并在发现这篇文章之后,我们找到了解决方案。这篇文章引用了错误的HOTFIX。要使TLS 1.2在Server 2008 R2上适用于.NET 2.0,您需要此HOTFIX:https://support.microsoft.com/zh-cn/help/3154518/support-for-tls-system-default-versions-included-内网框架

    它引用了3.5.1框架,但ALSO适用于2.0框架。安装此修复程序后,您可以按照指示在服务器上进行注册表更改,也可以在应用程序中进行代码更改以直接引用TLS 1.2。

    C#
    ServicePointManager.SecurityProtocol =(SecurityProtocolType)3072;

    VB
    ServicePointManager.SecurityProtocol = DirectCast(3072,System.Net.SecurityProtocolType)

    对于其他操作系统,请在此处查看Troy Starr的文章:
    https://community.qualys.com/thread/16917-net-framework

    希望这可以帮助


    对于找到此线程的其他人,我可以使用上面原始问题中详细介绍的相同步骤(#1-#4)使TLS 1.2在.Net 3.5上运行。将补丁应用到Win 2012服务器(我的共享托管公司可以很好地应用它)之后,然后在我的API调用之前添加了以下代码行以启用TLS 1.2,它立即起作用:

    1
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolTypeExtensions.Tls12;


    我意识到这个问题有点老了-但是在其他问题的帮助下-我们的Authorize.Net事务现在可以在.NET 2.0 AbleCommerce应用程序上与TLS 1.2一起使用。 [生产过渡期限似乎已延长至2018年2月28日]

    环境:Windows Server 2008 R2,IIS 7.5,AbleCommerce 7.0.2 build 11659,CommerceBuilder.AuthorizeNet 7.0.9764.0

    按照上述@JoeBoxer的回答,此链接起到了作用-专门为基于x64的系统设置两个注册表项(列出的补丁不会安装在我们的包装盒中):

    1
    2
    3
    4
    5
    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727]
    "SystemDefaultTlsVersions"=dword:00000001

    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v2.0.50727]
    "SystemDefaultTlsVersions"=dword:00000001

    我们也这样做了-因为我们没有TLS 1.2的条目-但这本身并不能解决问题。


    AS TLS 1.2不支持asp.net 2.0。
    有另一种方法可以实现TLS 1.2,而无需将项目从asp.net 2.0迁移到最新/更高版本。步骤如下:

  • 在更高版本的asp.net中创建一个新的单独项目。
  • 添加新的Web Service或WebAPI(稍后我们将在主项目中使用它)。
  • 在此处写下特定的代码,然后调用需要通过TLS 1.2进行验证的特定API。
  • 现在,部署此Web服务/ WebAPI并在主项目中使用。
  • 下面是示例代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    [WebMethod]
            public LoginResult TestLogin(string _username, string _password, string _tokenID)
            {
                try
                {
                    System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
                    LoginResult _loginResult = new LoginResult();
                    _loginResult = _sForceRef.login(_username, _password + _tokenID);
                    return _loginResult;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {

                }
            }
    Reference
    namespace System.Net
    {
        [System.Flags]
        public enum SecurityProtocolType
        {
           Ssl3 = 48,
           Tls = 192,
           Tls11 = 768,
           Tls12 = 3072,
        }
    }