Enable SSL in Visual Studio Development Server

Enable SSL in Visual Studio Development Server

In this article, I am going to discuss How to Enable SSL in Visual Studio Development Server. In our next article, I will discuss how to automatically redirect an HTTP request to HTTPS, when the request is made using HTTP to our Web API service.  Let us understand this with an example. First, create an empty Web API application with the name WebAPIEnableHTTP.

Once you create the project then add the following model class (Employee.cs) with the Models folder.

namespace WebAPIEnableHTTPS.Models
{
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
    }
}

Once you add the Model then you needs to add a Web API 2 Controller – Empty within the Controllers folder and name it as EmployeesController and then copy and paste the following code in it.

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebAPIEnableHTTPS.Models;

namespace WebAPIEnableHTTPS.Controllers
{
    public class EmployeesController : ApiController
    {
        List<Employee> employees = new List<Employee>()
        {
            new Employee() { EmployeeID = 101, EmployeeName = "Anurag"},
            new Employee() { EmployeeID = 102, EmployeeName = "Priyanka"},
            new Employee() { EmployeeID = 103, EmployeeName = "Sambit"},
            new Employee() { EmployeeID = 104, EmployeeName = "Preety"},
        };

        public IEnumerable<Employee> Get()
        {
            return employees;
        }

        public Employee Get(int id)
        {
            return employees.FirstOrDefault(s => s.EmployeeID == id);
        }
    }
}

At the moment when we navigate to the following URL, we get the output as expected.

http://localhost:55486/api/employees (please change the port number where your application is running),

Let’s change the protocol to https instead of HTTP and see what happened.

https://localhost:55486/api/employees

We get the following error page. This is because we have not enabled SSL for our Web API Service.

Enable SSL in Visual Studio Development Server

Let us discuss the steps to enable SSL in Visual Studio.

To enable SSL in Visual Studio please follow the below steps

In the Solution Explorer click on the WebAPIEnableHTTP Web API project and press F4 key on the keyboard which will open the Project Properties window. From the Project Properties window, we need to set the SSL Enabled property to true. As soon as we do this Visual Studio sets the SSL URL as shown in the image below.

Enable SSL in Visual Studio Development Server

As shown in the above image, once you set the SSL Enabled property to True, now you have two URLs one is SSL URL and the other one is the only URL.

The SSL URL is https://localhost:44300/

The URL is http://localhost:55486/

At this point, build the solution and then navigate to https://localhost:44300/api/employees URL in the browser and you will see the following browser security page. Make sure you click on the “Advanced” link to see the “Proceed to localhost” link. 

Enable SSL in Visual Studio Development Server

Once you click on the above Advanced tab it opens the following section within the same page.

Enable SSL in Visual Studio Development Server

Once you click on the Proceed to localhost (unsafe) tab, it will give you the response as shown in the image below.

Enable SSL in Visual Studio Development Server

As shown in the above image, once you click on the Not Secure link, you will see that the certificate is invalid as shown below.

Enable SSL in Visual Studio Development Server

The reason is that the certificate that Visual Studio installed automatically is not trusted. 

To solve the above problem, what we need to do is, we need to place the certificate that visual studio has issued in the Trusted Root Certificates folder

So please follow the below steps

Open the RUN window, then type mmc.exe and click OK as shown below

Enable SSL in Visual Studio Development Server

When you click on the OK button, one window will open, click “File” => “Add/Remove Snap-in” from that window and then from the “Available snap-ins” list select the “Certificates” and click on the “Add” button as shown in the below image

Enable SSL in Visual Studio Development Server

Once you click on the Add button it will open another screen from where select the “Computer account” radio button and then click on the next button as shown below

Enable SSL in Visual Studio Development Server


When you click on the Next button, it will open another screen and from that screen select the “Local computer” radio button and click on the “Finish” button as shown below.

Enable SSL in Visual Studio Development Server

Once you click on the Finish button, it will take you back to the Add or Remove snap-ins screen and from there click on the OK button as shown in the below image.

Enable SSL in Visual Studio Development Server

Expand the Console Root => Certificates (Local Computer) => Personal => Certificates folder and you will find a certificate that is Issued To localhost and Issued By localhost as shown in the image below.

Enable SSL in Visual Studio Development Server

Right click on the localhost certificate, and then select “All Tasks” and then click on the “Export” option as shown in the image below.

Enable SSL in Visual Studio Development Server

Once you click on the Export option, it will open the welcome to Welcome to Certificate Export Wizard screen and from there just click on the “Next” button.

From the next screen select the No, do not export the private key radio button and click on the Next radio button as shown below.

Enable SSL in Visual Studio Development Server


Once you click on the Next button, it will open the select File Format wizard and from that wizard select the “DER encoded binary X.509 (.CER)” radio button, and click on the Next button as shown in the below image.

Enable SSL in Visual Studio Development Server

From the next screen, provide a meaningful name (in my case I have given MyLocalhostCertificate) for the certificate that you are exporting and then click on the “Next” button. Once you click on the Next button, it will open the following window from there just click on the Finish button. Please remember the path where your certificate is stored. In my case, it is C:\Windows\System32\ MyLocalhostCertificate

Enable SSL in Visual Studio Development Server


Once you click on the Finish button, if everything is ok, then you will get the message Export Successful.

Importing the newly Generated Certificate in the Trusted Root Certification Folder:

Expand the Console Root – Certificates (Local Computer) – Trusted Root Certification Authorities – Certificates. And then right click on the “Certificates“, and select “All Tasks” and then select the “Import” option as shown below.

Enable SSL in Visual Studio Development Server

Click “Next” on the subsequent screen.

In the next screen, you have to enter the complete path where you have exported the certificate and then click on the click “Next” as shown below. In my case, the certificate is at C:\Windows\System32\MyLocalhostCertificate.cer

Enable SSL in Visual Studio Development Server

Once you click on the Next button, it will open another screen and from that screen select “Place all certificates in the following store” radio button and click on the “Next” button as shown below

Enable SSL in Visual Studio Development Server
Finally, click on the “Finish” button which will give you one message that import was successful. So that’s it. We have created and import the certificate for localhost in the trusted certificate location.

Now first close all the instances of the browser. Open a new browser instance and navigate to https://localhost:44300/api/employees and you will not get any certificate error. At the moment we can access the web API service using both HTTP and https.  

In the next article, I will discuss how to automatically redirect an HTTP request to HTTPS, when the request is made using HTTP to our Web API service.

SUMMARY

In this article, I try to explain how to enable SSL in Visual Studio Development Server step by step with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

No HTML was returned.

Leave a Reply

Your email address will not be published. Required fields are marked *