Skip to content

Instantly share code, notes, and snippets.

@davamix
Last active October 10, 2023 20:27
Show Gist options
  • Save davamix/4bedad09e2dcb53751c78c4a6e599859 to your computer and use it in GitHub Desktop.
Save davamix/4bedad09e2dcb53751c78c4a6e599859 to your computer and use it in GitHub Desktop.
Configure a self-signed certificate in Ubuntu to allow HTTPS access with an ASP.NET Core 2.1 application

Configure a self-signed certificate in Ubuntu to allow HTTPS access with an ASP.NET Core 2.1 application

  1. Create an asp.net core project as usual and check if you have access using HTTPS.
dotnet new web -o Sample
cd Sample
dotnet restore
dotnet run

1.1 Navigate to https://localhost:5001

Can you read the message "Hello World!"? Congrats, you can stop reading this guide.

Firefox shows you a "Secure Connection Failed" message? or do you have an error message in console like this?

dbug: HttpsConnectionAdapter[1]
      Failed to authenticate HTTPS connection.
System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Interop+Crypto+OpenSslCryptographicException: error:2006D002:BIO routines:BIO_new_file:system lib

Stay reading.

  1. Install 'ca-certificates' and 'openssl' packages
sudo apt-get install ca-certificates openssl
  1. Genereate the .pfx certificate with dotnet command [1]
dotnet dev-certs https -ep ${HOME}/.aspnet/https/aspnetapp.pfx -p crypticpassword
  1. Extract the .crt file [2]
cd ${HOME}/.aspnet/https/
openssl pkcs12 -in aspnetapp.pfx -nocerts -out aspnetapp.pfx
openssl pkcs12 -in aspnetapp.pfx -clcerts -nokeys -out aspnetapp.crt
  1. Copy the .crt file to the certificates location [3]
sudo cp aspnetapp.crt /usr/local/share/ca-certificates/
  1. Change the permissions to allow to read the certificate [4]
sudo chmod +r /usr/local/share/ca-certificates/*
  1. Run the application again and check the https address
dotnet run

Navigate to https://localhost:5001

If you have any error, you can check the links below to know more about each step.

LINKS

[1] https://github.com/dotnet/dotnet-docker/blob/master/samples/aspnetapp/aspnetcore-docker-https.md

[2] https://www.markbrilman.nl/2011/08/howto-convert-a-pfx-to-a-seperate-key-crt-file/

[3] https://stackoverflow.com/a/44160125

[4] https://github.com/dotnet/cli/issues/9376#issuecomment-393954876

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment