Linux Docker环境下ASP.NET Core 3.1 Kestrel配置HTTPS自签名证书报错求助
部署ASP.NET Core 3.1 Docker应用时HTTPS配置问题
场景说明
部署ASP.NET Core 3.1 Web应用到Linux机器的Docker容器中,使用Kestrel服务器,测试环境计划用自签名证书实现HTTPS。仅HTTP配置时运行正常,配置HTTPS后应用无法启动。
已完成的操作
- HTTP正常运行的docker-compose配置:
WebApp: container_name: webapp image: deploying-version ports: - "80:80" - "443:443" environment: - WebRootPath=/app volumes: - /home/data:/app/data - /home/Config/webapp/appsettings.json:/app/appsettings.json
- 开发机上成功的HTTPS配置:
导出Visual Studio生成的自签名证书后,在appsettings.json中添加:
"Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:5000" }, "Https": { "Url": "https://localhost:443", "Certificate": { "Path": "C:\\Temp\\localhost.pfx", "Password": "password" } } } }
- Linux主机生成证书及修改配置:
尝试生成自签名证书,命令顺序为:
openssl pkcs12 -export -out localhost.pfx -inkey localhost.key -in localhost.crt openssl req -config localhost.conf -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout localhost.key -days 3650 -out localhost.crt
修改docker-compose添加证书挂载:
volumes: # 其他挂载... - /home/Config/certs/localhost.pxf:/app/localhost.pfx
修改appsettings.json的Kestrel配置:
"Kestrel": { "Endpoints": { "Http": { "Url": "http://+:5000" }, "Https": { "Url": "https://+:443", "Certificate": { "Path": "app/localhost.pfx", "Password": "password" } } } }
报错信息
应用启动失败,抛出以下异常:
Interop+Crypto+OpenSslCryptographicException: error:0D06B08E:asn1 encoding routines:asn1_d2i_read_bio:not enough data at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromBio(SafeBioHandle bio, SafePasswordHandle password) at Internal.Cryptography.Pal.OpenSslX509CertificateReader.FromFile(String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password) at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.LoadCertificate(CertificateConfig certInfo, String endpointName) at Microsoft.AspNetCore.Server.Kestrel.KestrelConfigurationLoader.Load() at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.ValidateOptions() at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
解决方案
1. 重新生成有效的PFX证书
你生成证书的顺序错误,导致PFX文件为空。正确步骤如下:
- 第一步:创建证书配置文件
新建localhost.conf,内容如下:
[req] default_bits = 2048 prompt = no default_md = sha256 distinguished_name = dn [dn] C = CN ST = 你的省份 L = 你的城市 O = 你的组织名 OU = 你的部门 CN = localhost # 或应用访问域名
- 第二步:生成CRT和KEY文件
openssl req -config localhost.conf -new -x509 -sha256 -newkey rsa:2048 -nodes -keyout localhost.key -days 3650 -out localhost.crt
- 第三步:导出为PFX格式
设置证书密码(替换为你的密码):
openssl pkcs12 -export -out localhost.pfx -inkey localhost.key -in localhost.crt -passout pass:yourpassword
2. 修正路径错误
- docker-compose挂载修正:挂载路径中文件名拼写错误(
localhost.pxf改为localhost.pfx):
volumes: - /home/data:/app/data - /home/Config/webapp/appsettings.json:/app/appsettings.json - /home/Config/certs/localhost.pfx:/app/localhost.pfx
- appsettings.json路径修正:使用容器内绝对路径:
"Kestrel": { "Endpoints": { "Http": { "Url": "http://+:5000" }, "Https": { "Url": "https://+:443", "Certificate": { "Path": "/app/localhost.pfx", "Password": "yourpassword" } } } }
3. 确保证书可读取
设置主机上证书文件的权限,保证容器能读取:
chmod 644 /home/Config/certs/localhost.pfx
4. 重启容器
重新部署应用:
docker-compose down docker-compose up -d
内容的提问来源于stack exchange,提问作者user3901397
相关产品推荐
相关产品推荐

