In this article I will explain with an example, how to solve the error when using DownloadString method of the .Net WebClient class in C# and VB.Net.
Error: Could not create SSL/TLS secure channel
The above error is caused when TLS 1.2 protocol is not used before calling an API or Service which uses TLS 1.2 protocol for establishing secure channel for communication.
 
 
What is TLS 1.2?
TLS 1.2 (Transport Layer Security version 1.2) is more secure than other cryptographic protocols such as SSL 2.0, SSL 3.0, TLS 1.0, and TLS 1.1.
 
 
Downloading .Net Framework 4.5
You will need to download and install the .Net Framework runtime on your computer where you are developing the program in Visual Studio or where the application is installed.
Note: There is no need to upgrade the project to .Net 4.5. Only .Net 4.5 Framework needs to be installed and then the following technique can be used for setting the TLS1.2 in projects using .Net 2.0, .Net 3.0, .Net 3.5 and .Net 4.0.
 
 

Namespaces
You will need to import the following namespace.
C#
using System.Net;
 
VB.Net
Imports System.Net
 
 
Using TLS1.2 in .Net 4.5 or above
Inside the Page Load event, first the JSON string is downloaded from an API using DownloadString method of the WebClient class.
Finally, the JSON string is written to the Response.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json");
        Response.Write(json);
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim json As String = (New WebClient).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json")
        Response.Write(json)
    End If
End Sub
 
 
Using TLS1.2 in .Net 2.0, .Net 3.0, .Net 3.5 and .Net 4.0
Inside the Page Load event, first the JSON string is downloaded from an API using DownloadString method of the WebClient class.
Note: In .Net 4.5 Framework project, you will get option for TLS 1.2 but in Net 2.0, .Net 3.0, .Net 3.5 and .Net 4.0, the value 3072 needs to be set in the SecurityProtocol as shown below.
 
Finally, the JSON string is written to the Response.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json");
        Response.Write(json);
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
        Dim json As String = (New WebClient).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json")
        Response.Write(json)
    End If
End Sub
 
 
Downloads