【问题标题】:How to install .pfx certificate in windows docker image如何在 Windows Docker 映像中安装 .pfx 证书
【发布时间】:2020-02-28 12:14:22
【问题描述】:

我有一个使用 identityServer 4 的 .Net Core API,我正在尝试将 API 运行到 Docker Compose(Windows 容器)中,但由于以下异常而无法执行:

Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.

在谷歌上花了几个小时后,我发现了许多链接,其中首先安装证书,例如

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

**Docker - certificate not trusted**

     1. Delete the C:\Users{USER}\AppData\Roaming\ASP.NET\Https folder.
     2. Clean the solution. Delete the bin and obj folders. 
     3. Restart the Development tool. Visual Studio Code- 2019

在完成上述所有操作后面临同样的错误,我是不是在做错事。

这里是 dockerfile

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
user ContainerAdministrator

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1809 AS build
WORKDIR /src

COPY ../Certificate/idsrv3test.pfx .

COPY ["Tests-Identity/Tests-Identity.csproj", "Tests-Identity/"]
RUN dotnet restore "Tests-Identity/Tests-Identity.csproj"
COPY . .
WORKDIR "/src/Tests-Identity"
RUN dotnet build "Tests-Identity.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Tests-Identity.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Tests-Identity.dll"]

这里是 docker-compose.override.yml

version: '3.4'

services:
  tests-identity:
    environment:
       - ASPNETCORE_ENVIRONMENT=Development
       - ASPNETCORE_URLS=https://+:443;http://+:80

    ports:
      - "5000:80"
      - "5001:443"
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:C:\Users\ContainerUser\AppData\Roaming\Microsoft\UserSecrets:ro
      - ${APPDATA}/ASP.NET/Https:C:\Users\ContainerUser\AppData\Roaming\ASP.NET\Https:ro

【问题讨论】:

    标签: docker .net-core docker-compose dockerfile x509certificate


    【解决方案1】:

    穆尼尔 根据我的理解,以下命令只会在您在 IISEXPRESS 下运行应用程序时帮助您,这绝对不会帮助您,但我的理解是您正在尝试在“Docker Compose”项目下运行 API

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

    所以首先你需要在你的 docker 文件中删除 admin 容器用户

    user ContainerAdministrator 
    

    并删除此行

    COPY ../Certificate/idsrv3test.pfx .
    

    然后在“X509Certificate2”中添加以下参数,该参数将在您的 Certificate.cs 文件中

    请试试这个链接,它肯定会对你有很大帮助 https://github.com/dotnet/dotnet-docker/issues/863

    【讨论】:

    • 让我试试这个
    【解决方案2】:

    使用 Dockerfile 没有任何麻烦

    FROM ${DOCKER_REGISTRY}dotnet/core/sdk:3.1 AS build
    ARG VER
    
    WORKDIR /src
    COPY . .
    RUN dotnet restore --ignore-failed-sources
    RUN dotnet publish -c Release --version-suffix "%VER%" -o /app
    RUN dotnet dev-certs https -ep ./server.pfx -p 123
    

    【讨论】:

      【解决方案3】:

      要在 docker 构建过程中将证书(pfx 或其他)安装到 nanoserver 容器中,您需要使用 certoc.exe。 Certoc.exe 是 windows 服务器的一部分,您可以在 c:\windows\system32\certoc.exe 中的任何服务器上找到它。但是,它不附带 nanoserver。

      这是我用来将 CA 证书安装到受信任的根证书颁发机构(“根”)存储的 dockerfile 的一部分:

      RUN MKDIR "\temp"
      WORKDIR "/temp"
      COPY ["my-ca.cer","/temp"]
      COPY ["certoc.exe","/temp"]
      USER "ContainerAdministrator"
      RUN .\certoc -addstore root "c:\temp\my-ca.cer"
      RUN del /f /q .\certoc.exe
      RUN del /f /q .\my-ca.cer
      USER "ContainerUser"
      

      请注意,我以 ContainerAdmin 身份安装证书,但其他一切都以 ContainerUser 身份运行(这是最佳做法)。

      您可以调整以上内容以使用 certoc 导入 pfx 文件,使用

      USER "ContainerUser"
      COPY ["mycert.pfx","/temp"]
      RUN .\certoc -ImportPFX -p your_pfx_password_here "My" "c:\temp\mycert.pfx"
      

      将 pfx 导入到容器用户的个人(“我的”)存储中。

      【讨论】:

        猜你喜欢
        • 2018-04-19
        • 1970-01-01
        • 1970-01-01
        • 2021-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        相关资源
        最近更新 更多