How to Setup HTTPS with Proper SSL Certificate - Windows ASP.NET Core Hosting 2024 | Review and ComparisonWindows ASP.NET Core Hosting 2024 | Review and Comparison

You can reach the ASP.NET Core Kestrel web server at http://localhost:5000 because it is by default set up to operate at port 5000. On the other hand, you might need to use a different port or a https URL in production with the appropriate SSL setup. Kestrel endpoints offer a set of configuration parameters for that reason, enabling you to configure an infinite number of endpoints in the http and https protocols.

We’ll talk about https endpoints with configured SSL certificates here.

While you can use your program to accomplish this, working with appsettings.json is the recommended method. Endpoints can be loaded by Kestrel from an instance of IConfiguration. Endpoint configuration is done in Kestrel:Endpoints, and Kestrel configuration is loaded by default from the Kestrel section.

To become acquainted with Kestrel endpoints, add the following straightforward block to your entry if it doesn’t already exist in the Kestrel section. It can be added above the section for logging.

"Kestrel": {
    "Endpoints": {
      "httpEndpoint": {
        "Url": "http://localhost:5000 "
      },
    }
  }

Start your self-hosted.net core kestrel application now and modify the port value from 5000 to 5001 or another value. You can see that the default port has changed in the browser when you test with the new port. We have already added an endpoint for the http URL above, and we will add another one for the https URL right now. Place the block below the httpEndpoint.

  "httpsLocalEndpoint": {
   "Url": https://localhost:5001
 }

Restart your application and check the https URL in a web browser. If everything functions properly with the https port, proceed to the next step, where we will set up an appropriate SSL certificate for a custom domain address (your public URL, in this case). You have the option to use a certificate from your certificate store or configure a physical certificate that is kept on your hard drive.

Configuring PFX certificate stored locally in the hard disk
Assume for the moment that the.pfx file is kept in the same directory as the application. Now give it an additional endpoint.
"httpsPublicEndpoint": {
        "Url": "https://kestrel.demodooms.com:5002",
        "Certificate": {
          "Path": "star_demodooms_com.pfx",
          "Password": "Abcd!1234"
        }
     }
Setting up the local certificate store’s PFX certificate.
Assume that your PFX has been installed correctly in your Certificate store’s Personal store. Keep in mind that in the configuration, the store name must be My rather than Personal. Let’s add one more endpoint that is particular to this now.
"httpsPublicEndpoint2": {
        "Url": "https://kestrel2.demodooms.com:5003",
        "Certificate": {
          "Subject": "*.demodooms.com",
          "Store": "My",
          "Location": "LocalMachine"
        }
 }
Simply delete the Location entry where the default value is “current user” if you wish to use your location as the current user.
This is the full JSON configuration set with all four of our created endpoints.
{
  "Kestrel": {
    "Endpoints": {
      "httpEndpoint": {
        "Url": http://localhost:5000
      },
      "httpsLocalEndpoint": {
        "Url": https://localhost:5001
      },
      "httpsPublicEndpoint": {
        "Url": "https://kestrel.demodooms.com:5002",
        "Certificate": {
          "Path": "star_demodooms_com.pfx",
          "Password": "Abcd!1234"
        }
      },
      "httpsPublicEndpoint2": {
        "Url": "https://kestrel2.demodooms.com:5003",
        "Certificate": {
          "Subject": "*.demodooms.com",
          "Store": "My",
          "Location": "LocalMachine"
        }
      }
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Summary

It is pretty simple and easy, right? You have learned about how to setup end points in both http and https for your Asp.net core application. Happy coding!