有没有办法为一个WCF调用指定TLS 1.0,为单个应用程序中的另一个WCF或WebClient调用指定TLS 1.2?

时间:2016-07-13 21:07:58

标签: c# wcf ssl

我有一个需要调用多个Web服务的应用程序。 一个Web服务仅适用于TLS 1.0。 另一个禁用TLS 1.0。

有没有办法指定您希望在每个WCF Web服务调用中使用哪个TLS版本?

我能够使用

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

但它似乎强制推行这一设置。

.net 4.5.2

示例代码:

using System;
using System.Net;

namespace SSLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                Test();
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                Test();

            }
                Console.ReadKey();
            }


        static void Test()
        {
            try
            {
                Console.WriteLine(System.Net.ServicePointManager.SecurityProtocol);
                WebClient client = new WebClient();
                client.DownloadFile("https://webservices2.autotask.net/atservices/1.5/atws.wsdl", @"c:\code\test.wsdl");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

输出:

Tls12
System.Net.WebException: The request was aborted: Could not create SSL/TLS secur
e channel.
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at SSLTest.Program.Test() in c:\code\sandbox\SSLTest\SSLTest\SSLTest\Program.
cs:line 28
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls
Tls12
Tls

1 个答案:

答案 0 :(得分:1)

您可以指定多个安全协议,如下所示:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;