我想创建一个自定义环境变量名称:
ASPNETCORE_ENVIRONMENT=Local
这样做的原因是我希望本地计算机和开发服务器有不同的设置。
appsettings.Local.json
已按预期读取,但在调用app.Run();
时处理失败。
错误:
无法配置 HTTPS 端点。未指定服务器证书,默认开发者证书找不到或已过期。 要生成开发人员证书,请运行“dotnet dev-certs https”。要信任证书(仅限 Windows 和 macOS),请运行“dotnet dev-certs https --trust”。 有关配置 HTTPS 的更多信息,请参阅 https://go.microsoft.com/fwlink/?linkid=848054
即使执行上述命令,证书也没有成功生成,应用程序也无法运行。
运行命令
dotnet dev-certs https --trust --check --verbose
:
dotnet dev-certs https --trust --check --verbose
[1] Listing certificates from CurrentUser\My
[2] Found certificates: 1 certificate
1) 6D093AE1029D800C250535B9094AF57DF65DF510 - CN=localhost - Valid from 2023-03-14 13:42:52Z to 2024-03-13 13:42:52Z - IsHttpsDevelopmentCertificate: true - IsExportable: true
[3] Checking certificates validity
[4] Valid certificates: 1 certificate
1) 6D093AE1029D800C250535B9094AF57DF65DF510 - CN=localhost - Valid from 2023-03-14 13:42:52Z to 2024-03-13 13:42:52Z - IsHttpsDevelopmentCertificate: true - IsExportable: true
[5] Invalid certificates: no certificates
[6] Finished listing certificates.
[1] Listing certificates from CurrentUser\Root
[2] Found certificates: 1 certificate
1) 6D093AE1029D800C250535B9094AF57DF65DF510 - CN=localhost - Valid from 2023-03-14 13:42:52Z to 2024-03-13 13:42:52Z - IsHttpsDevelopmentCertificate: true - IsExportable: false
[3] Checking certificates validity
[4] Valid certificates: 1 certificate
1) 6D093AE1029D800C250535B9094AF57DF65DF510 - CN=localhost - Valid from 2023-03-14 13:42:52Z to 2024-03-13 13:42:52Z - IsHttpsDevelopmentCertificate: true - IsExportable: false
[5] Invalid certificates: no certificates
[6] Finished listing certificates.
A trusted certificate was found: 6D093AE1029D800C250535B9094AF57DF65DF510 - CN=localhost - Valid from 2023-03-14 13:42:52Z to 2024-03-13 13:42:52Z - IsHttpsDevelopmentCertificate: true - IsExportable: true
我找不到任何相关文档。
当
ASPNETCORE_ENVIRONMENT
是“开发”时,似乎会发生一些自动行为,我们需要在使用其他开发级自定义环境时复制这些行为。
在开发模式下,用户机密会自动包含在容器中。用户机密包括
Kestrel:Certificates:Development:Password
的键/值,这是在 docker 容器中使用证书所必需的。
要复制该行为,您需要手动配置应用程序以在自定义环境中包含用户密钥。
var builder = WebApplication.CreateBuilder(args);
if (builder.Environment.IsEnvironment("Local"))
{
builder.Configuration.AddUserSecrets<Program>();
}
来源:
https://github.com/dotnet/AspNetCore.Docs/issues/19359#issuecomment-916936673 https://github.com/dotnet/dotnet-docker/blob/main/samples/run-aspnetcore-https-development.md