在Windows docker容器中运行ASP.NET Core时出现此错误...

Unhandled Exception: System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.

我能够使它工作...
RUN dotnet dev-certs https

但是我想安装一个实际的证书...现在是一个自签名证书,但后来却是一个真实的证书。

因此,基于一篇名为“使用Powershell导入和绑定(bind)Windows容器中的SSL证书”的博客文章,我创建了以下PS脚本以在容器构建中运行...
Import-PfxCertificate -FilePath C:\Certificates\xxx.pfx -Password (ConvertTo-SecureString -String "xxxx" -AsPlainText -Force) `
-CertStoreLocation "Cert:\LocalMachine\My"

$cert = (Get-ChildItem -Path cert:\LocalMachine\My -DNSName "xxx.mydomain.com")[0]

$thumb =  ($cert | Select-Object Thumbprint)."Thumbprint"
$guid = [guid]::NewGuid().ToString("B")

netsh http add sslcert ipport=0.0.0.0:5001 certhash=$thumb certstorename=MY appid="$guid"

这样可以构建良好,并且似乎可以安装证书。但是,当我尝试运行容器时,出现了异常。我尝试使用443而不是5001,出现同样的错误

这是我的docker文件供引用...
 escape=`
 FROM xxx/base
 EXPOSE 5000
 EXPOSE 5001
 COPY testing/ /testing
 COPY service/ /Service
 COPY certificates/ /certificates
 COPY scripts/ /scripts

 # install certificates

 RUN scripts/Install-Container-Certificate.ps1

 # configure ASP.NET Core

 ENV ASPNETCORE_URLS http://+:80;https://+:443
 EXPOSE 80
 EXPOSE 443
 # RUN dotnet dev-certs https
 # start ASP.NET Core

 ENTRYPOINT ["dotnet","/Service/xxxService.dll"]

我究竟做错了什么?

最佳答案

如果要使用自签名证书在“开发”中运行,请遵循here文章。对于生产方案,请使用here

简而言之,建议的方法与您使用的方法不同,因为可以装入两个卷来引用包含证书和dotnet secret 的文件夹。

您可以将主机“%USER%\。aspnet\https”文件夹映射到 guest “/root/.aspnet/https/”,将主机“%APPDATA%\microsoft\UserSecrets\”文件夹映射到 guest “/root/.microsoft/usersecrets”。

生产和开发之间的主要区别在于,在生产中,您不会使用 secret ,您将需要传递包含证书和密码的文件夹,以便使用环境变量进行访问:

  • ASPNETCORE_Kestrel__证书__默认__路径
  • ASPNETCORE_Kestrel__证书__默认__密码

  • Kestrel将在Linux客户机上的“/root/.aspnet/https/”文件夹中查找与您的项目同名的证书。

    如果我使用您的appsettings.Development.json启用跟踪:

    “记录”:{
    “LogLevel”:{
    “Default”:“Trace”,
    “System”:“Trace”,
    “微软”:“踪迹”
    }
    }

    如果我在没有将证书安装在 guest 容器中的情况下开始运行示例应用程序,则会看到以下错误:

    root @ 7afc71f877ce:/app#dotnet helloworld.dll
    dbug:Microsoft.Extensions.Hosting.Internal.Host [1]
    托管开始
    dbug:Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer [2]
    无法在“/root/.aspnet/https/helloworld.pfx”处找到开发https证书。
    dbug:Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer [1]
    无法找到合适的开发https证书。
    暴击:Microsoft.AspNetCore.Server.Kestrel [0]
    无法启动红est。
    System.InvalidOperationException:无法配置HTTPS终结点。未指定服务器证书,并且找不到默认的开发人员证书或该证书已过期。
    要生成开发者证书,请运行“dotnet dev-certs https”。要信任证书(仅Windows和macOS),请运行'dotnet dev-certs https --trust'。

    希望能帮助到你。

    关于powershell - 无法在Windows docker容器中配置ASP.NET HTTPS终结点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52457514/

    10-16 22:33