如何强制 Visual Studio 为运行 Kestrel 的 .NET Core Web 应用程序重新创建 SSL 证书?

aut*_*ton 36 asp.net ssl-certificate visual-studio .net-core asp.net-core

创建 Web 项目时,Visual Studio 会自动生成 SSL 证书并提示您安装它。一切正常。

该证书现已过期,我不知道如何让它重新生成证书并重新开始该过程。我尝试localhost从证书存储中删除所有现有证书并删除 Secrets.json,但似乎没有什么可以强制它重新启动生成过程。

aut*_*ton 52

我终于弄明白了。

对于遇到此问题的其他人,解决此问题的步骤如下:

  • localhost必须在证书管理器中删除所有证书。它们可以在个人和受信任的根中找到
  • 必须删除 Secrets.json 文件。这可以在\Users\[user]\AppData\Roaming\Microsoft\UserSecrets\
  • 在powershell中,重新运行dotnet dev-certs https --trust创建并安装一个新的,并提示信任

此后它将再次起作用。

  • 我认为您可以使用“dotnet dev-certs https --clean”代替前两个步骤。 (24认同)

Ihd*_*ina 38

Visual Studio 2022 >工具> Nuget 包管理器>包管理器控制台

包管理器控制台显示出现在底部时,请键入以下命令

        PM > dotnet dev-certs https --clean
        //Cleaning HTTPS development certificates from the machine. A prompt might get displayed to confirm the removal of some of the certificates.
        //HTTPS development certificates successfully removed from the machine.

        PM > dotnet dev-certs https --trust
        //Trusting the HTTPS development certificate was requested.A confirmation prompt will be displayed if the certificate was not previously trusted.Click yes on the prompt to trust the certificate.
        //Successfully created and trusted a new HTTPS certificate.

        PM > dotnet dev-certs https --check
        //A valid certificate was found: C40087E6CA2F2A811F3BF78E3C5FE6BA8FA2XXXX - CN = localhost - Valid from 2023 - 01 - 27 23:21:10Z to 2024 - 01 - 27 23:21:10Z - IsHttpsDevelopmentCertificate: true - IsExportable: true
        //Run the command with both--check and --trust options to ensure that the certificate is not only valid but also trusted.
Run Code Online (Sandbox Code Playgroud)

详情请看这里

  • 我必须关闭所有 Chrome 窗口才能使其最终起作用 (8认同)

Ple*_*ebo 24

我花了几天时间试图解决这个问题,但上述答案都不适合我。在 Microsoft 管理控制台中搜索时,任何地方都不存在过期的证书。运行dotnet dev-certs https --clean--trust命令后,我的浏览器使用的证书仍然过期。

对我来说,解决方案是导航到%APPDATA%\ASP.NET\httpsC:\Users\{user}\AppData\Roaming\ASP.NET\https并删除那里的证书。他们应该有您的项目名称,即myproject.keymyproject.pem。删除这些文件,然后重建并运行您的项目。这应该会自动生成文件的新版本,并且浏览器应该会获取新证书。

感谢这篇文章提供了修复!

  • 干杯! 这对我有用。尝试所有其他解决方案后 (1) VS 2022 更新/修复 (2) 删除 certmgr 控制台中的所有本地主机证书 (3) Powershell 和/或 Nuget 控制台 [dotnet dev-certs https -clear, dotnet dev-certs https -trust, dotnet dev-certs https -check] 没有效果,过期的证书被拉入 IISExpress 开发运行。 (3认同)
  • 是的!如果没有提到这一步,上面的答案是不完整的。看来 `dotnet dev-certs https --clean` 命令也不完整,没有自动处理这个问题! (2认同)