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

问题描述 投票:0回答:4

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

该证书现已过期,我不知道如何让它重新生成证书并重新开始该过程。我尝试从证书存储中删除所有现有的

localhost
证书并删除 Secrets.json,但似乎没有什么可以强制它重新启动生成过程。

asp.net visual-studio asp.net-core .net-core ssl-certificate
4个回答
60
投票

我终于明白了。

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

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

此后又可以工作了。


48
投票

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

当底部出现 Package Manager Console 时,请键入以下命令

        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.

详情请看这里


30
投票

我花了几天时间试图解决这个问题,但上述答案都不适合我。在 Microsoft 管理控制台中搜索时,任何地方都不存在过期的证书。运行

dotnet dev-certs https --clean
--trust
命令后,我的浏览器使用的证书仍然过期。

我的解决方案是导航到

%APPDATA%\ASP.NET\https
C:\Users\{user}\AppData\Roaming\ASP.NET\https
并删除那里的证书。他们应该有您的项目名称,即
myproject.key
myproject.pem
。删除这些文件,然后重建并运行您的项目。这应该会自动生成文件的新版本,并且浏览器应该会获取新证书。

感谢这篇文章提供修复!


1
投票

Visual studio 生成相同过期证书的解决方案。

这不是问题的直接答案,而是与问题相关。对我来说,删除证书并让 Visual Studio 重新安装它后(删除证书并重新启动 VS 后提示我安装),我的旧过期证书再次重新生成(!)。

要解决此问题,您需要修复 IIS,请参阅此处了解更多详细信息修复 IIS。然后重新启动Visual Studio,启动调试器,它将生成一个新的证书。

© www.soinside.com 2019 - 2024. All rights reserved.