.Net Webapi – Unable to start Kestrel

While taking some Azure certifications, I have several times experienced this error during some Labs.

After creating a new webapi with

dotnet new webapi --output . --name SimpleApi

the ensuing

dotnet run

always fails with this error on my Windows 10 development pc.

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using ‘C:\Users\XXXX\AppData\Local\ASP.NET\DataProtection-Keys’ as key repository and Windows DPAPI to encrypt keys at rest.
crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run ‘dotnet dev-certs https’. To trust the certificate (Windows and macOS only) run ‘dotnet dev-certs https –trust’.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

The instructions on this page does not initially help much. As other people also experience, one dotnet dev-certs command says no valid certificates exist while another says that one already exists.
What have worked for me until now, is to first clear my pc of all existing dev certificates and then generate a new one. I seem to have to do this everytime i create a new project…

dotnet dev-certs https –clean
dotnet dev-certs https –trust

I always comment out the app.UseHttpsRedirection(); in the autogenerated Startup.cs class, but it does not make any difference.