Self-signed certificate on mac

Posted by Daniel on Wednesday, March 15, 2023

If you have created a self-signed certificate with .NET CLI, it only seems to work with localhost, and not custom domains. To install a self-signed certificate and use with a custom domain on Kestrel you could follow theese steps to make it work on mac.

Solution

This solution assumes you have brew installed. If not you could install it with following command

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install mkcert via brew

brew install mkcert

Install the local CA in the system trust store

mkcert -install

Create self-signed certificate .pem and key.pem files with following command, where you pass in the domain you want the certicate for.

mkcert "*.local" localhost 127.0.01 ::1

This should generate an output like this

Created a new certificate valid for the following names 📜
 - "*.local"
   Warning: many browsers don't support second-level wildcards like "*.local" ⚠ī¸
 - "localhost"
 - "127.0.01"
 - "::1"

Reminder: X.509 wildcards only go one level deep, so this won't match a.b.local ℹī¸

The certificate is at "./_wildcard.local+3.pem" and the key at "./_wildcard.local+3-key.pem" ✅

Now you have created the self-signed certifate and need to tell Kestrel to use it, which you can do by adding following configuration in appsettings.json.

  "Kestrel": {
    "Certificates": {
      "Default": {
        "Path": "./_wildcard.local+3.pem",
        "KeyPath": "_wildcard.local+3-key.pem"
      }
    }
  }

comments powered by Disqus