无法在Windows docker容器中配置ASP.NET HTTPS终结点

AQu*_*rky 6 powershell docker asp.net-core

在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 Code Online (Sandbox Code Playgroud)

我能够使它工作...

RUN dotnet dev-certs https
Run Code Online (Sandbox Code Playgroud)

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

因此,基于一篇名为“使用Powershell导入和绑定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"
Run Code Online (Sandbox Code Playgroud)

这样可以构建良好,并且似乎可以安装证书。但是当我尝试运行容器时出现异常。我尝试使用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"]
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Alb*_*rto 0

如果您想使用自签名证书在开发中运行,您可以按照此处的文章进行操作。对于生产场景,请改为此处

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

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

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

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

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

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

      “记录”:{
        “日志级别”:{
          “默认”:“跟踪”,
          "系统": "踪迹",
          “微软”:“追踪”
        }
      }

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

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

希望能帮助到你。