Asp.net core keep using the expired certificate

Viewed 5843

Recently, my localhost certificate is expired, I have gone to "sertmgr.msc" remove all localhost certificate and restart the VS and add a new localhost certificate to windows. But when am I running my application again, still use the old expired certificate not the new one, does anyone know how to fix that? I have already run the command show below.

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

Expired Cert Cert In Cert Manage

4 Answers

I managed to hack my way around this issue:

  1. Before you do anything, clean the old certificate and generate a new trusted one.

    dotnet dev-certs https --clean
    dotnet dev-certs https --trust
    
  2. And if the process above fails, manually remove the certificates before retrying the clean/trust commands.

  3. Get the User Secrets ID of the Web Application you're having trouble with. Search for UserSecrets.UserSecretsIdAttribute(" in your project folder and take the GUID.

  4. Go to %APPDATA%\Microsoft\UserSecrets and open the folder containing the GUID of the problematic project you are struggling with and leave it open.

  5. Create a new Asp .Net Core project, get its secrets GUID, go to the corresponding secret folder (%APPDATA%\Microsoft\UserSecrets\GUID), and open the file secrets.json. You should see something like this

    {
      "Kestrel:Certificates:Development:Password": "8353f2ec-3cc0-4052-9776-9585b6abd346"
    }
    
  6. Copy that setting from the newly created project secrets.json and use it to override the development password on the secrets.json of the broken project

This way, I've managed to get my old project to use the newly generated certificate. It is hacky, but it works

I assume you're using IIS Express to host your application. If so, it sounds like you're missing linking the certificate to your application(s) as described in this blog post:

Go to C:\Program Files (x86)\IIS Express and run the following from the command line, entering the proper port number and the new certificate thumbprint:

IisExpressAdminCmd.exe setupsslUrl -url:https://localhost:PORT/ -CertHash:THUMB

Based on Pedro's answer,

I used:

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

to clean/replace the old certs.

Then I went to:

%APPDATA%\Microsoft\UserSecrets

And deleted all the folders found there.

Now my application runs without complaining. Creating a new .net core 6 application and running it didn't create a new folder, so I don't know if it's no-longer required.

If above correct instruction doesn't work you can check below steps:

  1. Open Run and open mmc.exe
  2. Inside MMC from File menu click on Add/Remove Snap-in.
  3. In the add/remove snap-in window, find certificates in available snap-ins and add it to the selected
  4. Pick User account
  5. In the console root -> Certificates Current User -> Personal click on Certificates
  6. You will see the list of issued and installed certificates for the current user. DON’T remove or change any certificates you don’t know, only remove certificates related to self-sign localhost ASP.NET Core.
Related