How to Enable SSL in Visual Studio Development Server?
In this article, I am going to discuss How to Enable SSL in the Visual Studio Development Server with an example. While developing any application if you want to test the service using https protocol then you need to enable SSL in visual studio. Let us understand how to enable SSL in Visual Studio with an example.
Creating an Empty Application:
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.
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 error page as This site can’t provide a secure connection. This is because we have not enabled SSL for our Web API Service.
How to Enable SSL in Visual Studio Development Server?
To enable SSL in the Visual Studio development server, you need to 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 you do this Visual Studio sets the SSL URL as shown in the image below.
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/ and 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.
Once you click on the above Advanced tab it opens the following section on the same page.
Once you click on the Proceed to localhost (unsafe) tab, it will give you the response as shown in the image below.
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.
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
Generating a Trusted Certificate:
In order to use a trusted certificate, please follow the below steps
Open the RUN window, then type mmc.exe and click on the OK button as shown below
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
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
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.
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.
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.
Right-click on the localhost certificate, and then select “All Tasks” and then click on the “Export” option as shown in the image below.
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.
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.
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
Once you click on the Finish button, if everything is ok, then you will get the message Export Successful.
How to Import 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.
Click on the “Next” button 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
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
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 the 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 am going to discuss how to automatically redirect an HTTP request to HTTPS, when the request is made using HTTP to our Web API service. Here, in this article, I try to explain how to enable SSL in the Visual Studio Development Server step by step with an example. I hope this article will help you with your need.
Summary:
I hope this post will be helpful to understand how to enable SSL in visual studio
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
In this article, I am going to discuss How to Enable SSL in the Visual Studio Development Server with an example. While developing any application if you want to test the service using https protocol then you need to enable SSL in visual studio. Let us understand how to enable SSL in Visual Studio with an example.
Creating an Empty Application:
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 need 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 error page as This site can’t provide a secure connection. This is because we have not enabled SSL for our Web API Service.
How to Enable SSL in Visual Studio Development Server?
To enable SSL in the Visual Studio development server, you need to 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 you do this Visual Studio sets the SSL URL as shown in the image below.
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/ and 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.
Once you click on the above Advanced tab it opens the following section on the same page.
Once you click on the Proceed to localhost (unsafe) tab, it will give you the response as shown in the image below.
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.
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
Generating a Trusted Certificate:
In order to use a trusted certificate, please follow the below steps
Open the RUN window, then type mmc.exe and click on the OK button as shown below
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
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
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.
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.
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.
Right-click on the localhost certificate, and then select “All Tasks” and then click on the “Export” option as shown in the image below.
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.
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.
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
Once you click on the Finish button, if everything is ok, then you will get the message Export Successful.
How to Import 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.
Click on the “Next” button 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
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
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 the 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 am going to discuss how to automatically redirect an HTTP request to HTTPS, when the request is made using HTTP to our Web API service. Here, in this article, I try to explain how to enable SSL in the Visual Studio Development Server step by step with an example. I hope this article will help you with your need.
Summary:
I hope this post will be helpful to understand how to enable SSL in visual studio
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.