How to Setup SSL in ASP.NET Application - Windows ASP.NET Core Hosting 2024 | Review and ComparisonWindows ASP.NET Core Hosting 2024 | Review and Comparison

This post will walk us through the process of configuring SSL in ASP.NET. Secure Sockets Layer (SSL) website security is essential for safeguarding sensitive data, including credit card numbers and login credentials. We will go over how to configure SSL in an ASP.NET Core website in this article.

How to setup HTTPS in ASP.NET applications

1. Get an SSL certificate

Getting a certificate from an authorised certificate authority (CA) is the first step in setting up SSL. The connection between the client and server will be encrypted using this certificate.

An SSL certificate can be obtained in a number of ways, such as:

  • Purchase from a Certificate Authority (CA):Buying an SSL certificate from a trustworthy CA is one of the most popular ways to get one. Comodo, GlobalSign, and DigiCert are a few well-known CAs. You will need to supply your domain name and other organization details when buying a certificate. After confirming your identification, the CA will issue the certificate.
  • Generate a self-signed certificate: A self-signed certificate can also be created. While a self-signed certificate is not trusted by browsers, this is helpful for testing but not advised for production use.
  • Use a free SSL service like Let’s Encrypt: Another option is to obtain a free SSL certificate from Let’s Encrypt, which is a free, automated, and open certificate authority that provides SSL/TLS certificates to enable HTTPS on the web.

To let you follow along without having to buy CAs, I’ll construct a new web application and decide to generate a self-signed certificate specifically for this tutorial. Thus, to generate a certificate file for you, perform the following command.

dotnet dev-certs https -ep certificate.pfx -p password

You can use a new password and a custom route in place of the certificate.pfx.

To avoid privacy errors like this:

You can run the following command afterward:

dotnet dev-certs https --trust

This command only works for Windows.

Installing an SSL certificate on your server and setting up your website to use it are the next steps after obtaining one.

2. Set up SSL on your Asp.net website

You must set up the application to use SSL in the Program.cs section of your ASP.NET Core project. To accomplish this, include the subsequent code in the Configure method:

app.UseHttpsRedirection();

All HTTP queries will be automatically redirected to HTTPS as a result.

3. Provide the SSL certificate

You will need to give the program the SSL certificate in the same Program.cs file. To accomplish this, place the following code before var app = builder.Build(); and after var builder = WebApplication.CreateBuilder(args);:

var httpsConnectionAdapterOptions = new HttpsConnectionAdapterOptions
{
    SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
    ClientCertificateMode = ClientCertificateMode.AllowCertificate,
    ServerCertificate = new X509Certificate2("./certificate.pfx", "password")

};
builder.WebHost.ConfigureKestrel(options =>
    options.ConfigureEndpointDefaults(listenOptions =>
    listenOptions.UseHttps(httpsConnectionAdapterOptions)));
Make sure you enter the real path to your SSL certificate in lieu of the placeholder./certificate.pfx, and change the password with the one from the certificate.

4. Test your SSL configuration

It’s crucial to test your website’s SSL setup after setting it up to make sure everything is operating as it should. To accomplish this, use the https:// protocol to access your website and look for the padlock icon in the address bar of your browser.

5. Keep your SSL certificate up to date

There are dates on which SSL certificates expire, therefore be careful to renew the certificate in advance to prevent any service interruptions.

How Does HTTPS Work?

A secure connection is established between a web server and a web browser through the HTTPS (HTTP Secure) protocol. Built on top of the HTTP protocol, HTTPS encrypts data being communicated between the server and browser using SSL/TLS (Secure Sockets Layer/Transport Layer Security).

This is a quick synopsis of the HTTPS procedure:

  • To create a secure connection, the web browser (client) makes a request to the webserver.
  • The public key of the web server is included in the SSL/TLS certificate that it returns.
  • The SSL/TLS certificate is authenticated by the web browser by comparing it to a trusted certificate authority (CA) list.
  • The SSL/TLS handshake is started by the server and web browser after the certificate has been validated.
  • After deciding on a set of encryption techniques, the web browser and server generate a special symmetric key that will be used to encrypt any data sent back and forth between them.
  • The web browser will submit an HTTP request across the secure SSL/TLS connection after the SSL/TLS session has been created.
  • After processing the request, the web server will provide an HTTP response that has been encrypted with the symmetric key.
  • After the web browser has decrypted the answer, the user will see the content.

Conclusion

You may secure critical data while it’s in transit by configuring SSL on your ASP.NET Core website by following these instructions. It is crucial to remember that keeping your website secure is a continuous process that calls for frequent upgrades and monitoring.

HTTPS is far more secure than standard HTTP since all data is encrypted during transmission between the browser and server, making it impossible for third parties to intercept or read. Additionally, a padlock icon in the address bar of the browser and the prefix “https” in the URL designate websites that employ HTTPS.