如何在Windows容器的ASPNET:3.0基本映像中安装根证书

问题描述

我正在从Windows 2019服务器托管两个Windows容器,并且两个都在https中运行。当我的Web URL容器尝试调用API容器时。它不起作用,当我进入Web容器并向我的API网站运行curl命令时,我收到以下错误

(77)通道:下一个InitializeSecurityContext失败:SEC_E_UNTRUSTED_ROOT(0x80090325)-证书链是由不受信任的颁发机构颁发的。

我试图找出如何将根证书导入到我的aspnet:3.0基本映像中。

解决方法

这就是我要使证书在Docker容器中工作的方式。我导入了根证书以及该应用程序的证书,因此请准备所需的部分。

  1. 在主机上,将证书(pfx)放在目录中,并将其安装在容器中。我将假定您已将它们安装在容器中的“ C:\ certificates”上。

  2. 我将证书作为环境变量传递,以便脚本可以提取它们。

  3. 添加此脚本以在容器启动时运行:

Import-Module WebAdministration
$webCertificatePath = $ENV:WEB_CERTIFICATE_PATH
$webCertificatePassword = $ENV:WEB_CERTIFICATE_PASSWORD
$rootCertificatePaths = $ENV:ROOT_CERTIFICATE_PATHS
# I don't do anything with this... yet
$intermediateCertificatePaths = $ENV:INTERMEDIATE_CERTIFICATE_PATHS

# Import Root Certificate
# You can stop here if you only want the Root Certificate installed.
Import-Certificate -FilePath $rootCertificatePaths -CertStoreLocation 'Cert:\\LocalMachine\Root'

# Import website certificate
$mypwd = ConvertTo-SecureString -String $webCertificatePassword -Force -AsPlainText
$cert = Import-PfxCertificate -FilePath "$webCertificatePath" -Password $mypwd -CertStoreLocation 'Cert:\LocalMachine\My'
if (-not (Test-Path 'IIS:\SSLBindings\0.0.0.0!443')) {
  $cert | New-Item 'IIS:\SSLBindings\0.0.0.0!443'
}

有很多方法可以做到这一点,但是您明白了。