Could be a few reasons for this issue. I found three while trying to fix mine.
1. Something wrong with dotnet.
When you run the command:
dotnet dev-certs https
The usual response must be something like that:
% dotnet dev-certs https
The HTTPS developer certificate was generated successfully.
And when you run the command
dotnet dev-certs https --trust
You must get a response which looks like:
% dotnet dev-certs https --trust
Trusting the HTTPS development certificate was requested. If the certificate is not already trusted we will run the following command:
'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain <<certificate>>'
This command might prompt you for your password to install the certificate on the system keychain.
A valid HTTPS certificate is already present.
Here, the last line could either say A valid HTTPS certificate is already present.
or it could say that the certificate was trusted successfully, depending on whether you executed the command once or more than that.
However, if running these commands give a result that look like this:
It means that there is definitely something wrong with your version of dotnet. I suggest to uninstall dotnet from your mac, and try to reinstall it from the official website.
Here is a quick and effective way to uninstall dotnet:
https://stackoverflow.com/a/44089766/1928149
Now try adding the certificates again.
2. Dotnet may not be able to access the keychain.
In this case, run the following two commands:
dotnet dev-certs https --check
dotnet dev-certs https --clean
Now, just go to your Keychain Access
app in macbook, search for certificates with the name localhost
and delete them. (You can also drag and drop it to one of your folders before deleting, in case you need a backup.)
Once you have removed all certificates, run the following:
dotnet dev-certs https
dotnet dev-certs https --trust
Now, when you run the following:
dotnet dev-certs https --check --trust
You should find a certificate which is available as well as trusted.
3. CryptographicException while reading the certificates.
Run the following command:
dotnet dev-certs https -v
This should display some debug information about your certificates. If the output gives an exception which is something like:
System.Security.Cryptography.CryptographicException certificate 'certificate-name` is corrupted
or
System.Security.Cryptography.CryptographicException: ASN1 corrupted data
This is how it looked for me:

This means dotnet is having issues in reading certificates from your keychain because of a particular unrelated certificate. Go to your Keychain Access
app and look for the certificate certificate-name
as shown in the logs above. Keep a backup of this certificate and delete it from your Keychain Access
app. Try to run the following command again:
dotnet dev-certs https -v
Hopefully, it lists certificates without any exceptions now.
Now, follow Point #2
above and hopefully things should now work.