情境:
在 .NET Framework 4.0 中沒有SecurityProtocolType.Tls1.2 的列舉型態可以使用,要在 .NET Framework 4.5 以上的版本才有 Tls1.2 可以使用。
參考資訊:
.NET Framework 4.0 中沒有SecurityProtocolType.Tls1.2
https://msdn.microsoft.com/en-us/library/system.security.authentication.sslprotocols(v=vs.100).aspx
.NET Framework 4.5中有SecurityProtocolType.Tls1.2
https://msdn.microsoft.com/en-us/library/system.security.authentication.sslprotocols(v=vs.110).aspx
寫到這裡,好像要使用TLS1.2 程式一定要升版到 .NET Framework 4.5 或更新版本?
提供一個範例通用在 .NET 4.0 以上的版本:
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create(“https://ebilltest.fisc.com.tw/cpp/download/ebillwebservice.wsdl.xml“);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.ServerCertificateValidationCallback = newRemoteCertificateValidationCallback(CheckValidationResult);
// 重點是修改這行
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}
}