Ale*_*xei 6 https ssl-certificate docker asp.net-core
我正在尝试将 Docker 用于现有应用程序,但遇到以下问题。当 API 尝试从容器中获取 Identity Server 元数据时,它失败并显示以下内容:
web_api | System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://host.docker.internal:5500/.well-known/openid-configuration'.
web_api | ---> System.IO.IOException: IDX20804: Unable to retrieve document from: 'https://host.docker.internal:5500/.well-known/openid-configuration'.
web_api | ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
web_api | ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
Run Code Online (Sandbox Code Playgroud)
这确实由主机浏览器确认(Chrome 中的认证错误)。
如果我使用 localhost 而不是 host.docker.internal 访问相同的元数据,它会按预期工作。
我已使用此处的说明来创建和信任身份服务器也使用的本地主机证书:
dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\aspnetapp.pfx -p { password here }
dotnet dev-certs https --trust
Run Code Online (Sandbox Code Playgroud)
我假设这些说明仅为本地主机创建证书,但我试图获得一个也适用于 host.docker.internal 的解决方案。
问题:如何允许从本地主机和容器到 ASP.NET Core Web API 应用程序的 HTTPS 连接?
我认为你是对的 -dotnet dev-certs
只为localhost
. 据我所知是不可配置的。因此,您似乎必须生成自己的自签名证书并信任它。假设您使用的是 Windows,一种方法是使用Powershell 的 New-SelfSignedCertificate:
#create a SAN cert for both host.docker.internal and localhost
$cert = New-SelfSignedCertificate -DnsName "host.docker.internal", "localhost" -CertStoreLocation cert:\localmachine\my
#export it for docker container to pick up later
$password = ConvertTo-SecureString -String "123123" -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath C:\https\aspnetapp.pfx -Password $password
# trust it on your host machine
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "TrustedPublisher","LocalMachine"
$store.Open("ReadWrite")
$store.Add($cert)
$store.Close()
Run Code Online (Sandbox Code Playgroud)
假设您为应用程序使用 Microsoft 提供的基本映像,要提示 Kestrel 选择新证书,您可能必须像这样运行 docker:
docker pull your_docker_image
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="123123" -e ASPNETCORE_Kestrel__Certificates__Default__Path=\https\aspnetapp.pfx -v %USERPROFILE%\.aspnet\https:C:\https\ your_docker_image
docker run <your image> --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_Kestrel__Certificates__Default__Password="123123" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
Run Code Online (Sandbox Code Playgroud)
注意我正在导出证书C:\https
,然后将其安装到容器上。
您可能需要处理路径和域名,但希望这可以为您提供一个起点。
OpenSSL是另一种可能的解决方案,它也是跨平台的
UPD由于 Docker 机器通常是 Linux,所以这个答案可能不是一个完整的解决方案。查看我关于同一主题的其他答案- 该答案利用 OpenSSL 来执行任务,并介绍如何在构建时将自签名证书嵌入到 Docker 映像中。
归档时间: |
|
查看次数: |
1931 次 |
最近记录: |