Setting up HTTPS in core 1.1

By | Mar 24, 2018

Securing a website with HTTPS in core is a bit different than with normal in IIS. In this post I will show to configure core so that it uses HTTPS.

How to apply HTTPS in core 1.1

I have been working on an core site and will be showing some of the code snippets and steps needed.

  1. We start in the program.cs Main method where we need to load the JSON file which will contain the certificate information.
    var config = new ConfigurationBuilder()
    .AddJsonFile("hostingOptions.json", true, true)
    • Define a configuration builder.
    • Specify the base path to use. In this example it will be the current directory.
    • Specify that we are adding a JSON file, in this example it is called hostingOptions.js. The file is optional and the application should be reloaded if the file changes. The JSON files not exist at this point.
  2.  Next add a JSON file into the root of the website and call it “hostingOptions.json” with the following structure
    • The server.urls represents the secure url of the website.
    • certificate.location is where the pfx file can be found in relation to the website.
    • certificate.password is obvious – its the password for the pfx.
  3. Next create the Certificates folder and copy your certificate pfx file into it.
  4. Back to the program.cs Main method. After the configuration builder, load the certificate with the following syntax.
    X509Certificate2 siteCertificate = new X509Certificate2(config["certificate.location"], config["certificate.password"]);
  5. Now we are ready to configure the host. I am using Kestrel.
    var host = new WebHostBuilder()
    .UseKestrel(options =>
    • Instruct the web host to use HTTP and assign the certificate to use.

The completed method will look like this.

Here is another article, If you are looking for more information related to the security of core

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments