有不同方式可以建立和使用自我簽署憑證,以用於開發和測試案例。 本文涵蓋使用自我簽署憑證與dotnet dev-certs
,以及其他選項如PowerShell
和OpenSSL
。
接著,您可以使用容器中裝載 的 ASP.NET Core 應用程式 等範例來驗證憑證是否載入。
先決條件
針對 dotnet dev-certs
,請務必安裝適當的 .NET 版本:
此範例需要 Docker 17.06 或更新版本的 Docker 用戶端 。
準備範例應用程式
針對本指南,您將使用 範例應用程式 ,並在適當時進行變更。
檢查範例應用程式 Dockerfile 是否使用 .NET 8。
視主機 OS 而定,您可能需要更新 ASP.NET 運行時間。 例如,若要以適當的 Windows 執行時間為目標,請在 Dockerfile 中變更 mcr.microsoft.com/dotnet/aspnet:8.0-nanoservercore-2009 AS runtime
為 mcr.microsoft.com/dotnet/aspnet:8.0-windowsservercore-ltsc2022 AS runtime
。
例如,這有助於在 Windows 上測試憑證:
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore -r win-x64
# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /source/aspnetapp
RUN dotnet publish -c release -o /app -r win-x64 --self-contained false --no-restore
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0-windowsservercore-ltsc2022 AS runtime
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["aspnetapp"]
如果您要在Linux上測試憑證,您可以使用現有的 Dockerfile。
請確定 aspnetapp.csproj
包含適當的目標架構:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<!--Other Properties-->
</PropertyGroup>
</Project>
備註
如果您想使用 dotnet publish
參數 來精簡 部署,請確定包含適當的相依性,以支援 SSL 憑證。 更新 dotnet-docker\samples\aspnetapp\aspnetapp.csproj 檔案,以確保容器中包含適當的元件。 如需參考,請檢查如何在使用「修剪」進行自包式部署時更新 .csproj 檔案 以支援 SSL 憑證 。
請確定您指向範例應用程式。
cd .\dotnet-docker\samples\aspnetapp
建置容器以在本機進行測試。
docker build -t aspnetapp:my-sample -f Dockerfile .
建立自我簽署憑證
您可以建立自我簽署憑證:
使用 dotnet dev-certs工具
您可以使用 dotnet dev-certs
來處理自我簽署憑證。
dotnet dev-certs https -ep $env:USERPROFILE\.aspnet\https\aspnetapp.pfx -p crypticpassword
dotnet dev-certs https --trust
備註
在此案例中, 憑證名稱 aspnetapp.pfx 必須符合專案元件名稱。
crypticpassword
作為您自己選擇之密碼的佔位符。 如果主控台傳回「有效的 HTTPS 憑證已存在」,則存放區中已有受信任的憑證。 您可以使用 MMC 控制台匯出它。
設定應用程式的機密以用於憑證:
dotnet user-secrets -p aspnetapp\aspnetapp.csproj init
dotnet user-secrets -p aspnetapp\aspnetapp.csproj set "Kestrel:Certificates:Development:Password" "crypticpassword"
備註
注意:密碼必須符合憑證所使用的密碼。
使用針對 HTTPS 設定的 ASP.NET Core 執行容器映像:
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_ENVIRONMENT=Development -v $env:APPDATA\microsoft\UserSecrets\:C:\Users\ContainerUser\AppData\Roaming\microsoft\UserSecrets -v $env:USERPROFILE\.aspnet\https:C:\Users\ContainerUser\AppData\Roaming\ASP.NET\Https mcr.microsoft.com/dotnet/samples:aspnetapp
應用程式啟動時,請在網頁瀏覽器中瀏覽至 https://localhost:8001
。
收拾整理
如果秘密和憑證未使用中,請務必加以清除。
dotnet user-secrets remove "Kestrel:Certificates:Development:Password" -p aspnetapp\aspnetapp.csproj
dotnet dev-certs https --clean
透過 PowerShell
您可以使用PowerShell來產生自我簽署憑證。 PKI 用戶端可用來產生自我簽署憑證。
$cert = New-SelfSignedCertificate -DnsName @("contoso.com", "www.contoso.com") -CertStoreLocation "cert:\LocalMachine\My"
系統會產生憑證,但為了進行測試,應該放在憑證存放區中,以在瀏覽器中進行測試。
$certKeyPath = "c:\certs\contoso.com.pfx"
$password = ConvertTo-SecureString 'password' -AsPlainText -Force
$cert | Export-PfxCertificate -FilePath $certKeyPath -Password $password
$rootCert = $(Import-PfxCertificate -FilePath $certKeyPath -CertStoreLocation 'Cert:\LocalMachine\Root' -Password $password)
此時,憑證應該可從 MMC 插件檢視。
您可以在適用於 Linux 的 Windows 子系統中執行範例容器 (WSL):
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_Kestrel__Certificates__Default__Password="password" -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/contoso.com.pfx -v /c/certs:/https/ mcr.microsoft.com/dotnet/samples:aspnetapp
備註
請注意,使用磁碟區映射時,檔案路徑可能會依據主機的不同特性被不同地處理。 例如,在 WSL 中,您可以將 /c/certs 取代為 /mnt/c/certs。
如果您使用稍早針對 Windows 建置的容器,執行命令看起來會像下面這樣:
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_Kestrel__Certificates__Default__Password="password" -e ASPNETCORE_Kestrel__Certificates__Default__Path=c:\https\contoso.com.pfx -v c:\certs:C:\https aspnetapp:my-sample
應用程式啟動之後,瀏覽至瀏覽器中的 contoso.com:8001。
請確保主機條目已更新,使 contoso.com
能在適當的 IP 位址上回應(例如 127.0.0.1)。 如果無法辨識憑證,請確定與容器一起載入的憑證也會在主機上受到信任,而且有適當的 SAN 和 DNS 條目,contoso.com
。
收拾整理
$cert | Remove-Item
Get-ChildItem $certKeyPath | Remove-Item
$rootCert | Remove-item
使用 OpenSSL
您可以使用 OpenSSL 來建立自我簽署憑證。 此範例使用 WSL / Ubuntu 和 bash shell 搭配 OpenSSL
。
此命令會產生 .crt 和 .key。
PARENT="contoso.com"
openssl req \
-x509 \
-newkey rsa:4096 \
-sha256 \
-days 365 \
-nodes \
-keyout $PARENT.key \
-out $PARENT.crt \
-subj "/CN=${PARENT}" \
-extensions v3_ca \
-extensions v3_req \
-config <( \
echo '[req]'; \
echo 'default_bits= 4096'; \
echo 'distinguished_name=req'; \
echo 'x509_extension = v3_ca'; \
echo 'req_extensions = v3_req'; \
echo '[v3_req]'; \
echo 'basicConstraints = CA:FALSE'; \
echo 'keyUsage = nonRepudiation, digitalSignature, keyEncipherment'; \
echo 'subjectAltName = @alt_names'; \
echo '[ alt_names ]'; \
echo "DNS.1 = www.${PARENT}"; \
echo "DNS.2 = ${PARENT}"; \
echo '[ v3_ca ]'; \
echo 'subjectKeyIdentifier=hash'; \
echo 'authorityKeyIdentifier=keyid:always,issuer'; \
echo 'basicConstraints = critical, CA:TRUE, pathlen:0'; \
echo 'keyUsage = critical, cRLSign, keyCertSign'; \
echo 'extendedKeyUsage = serverAuth, clientAuth')
openssl x509 -noout -text -in $PARENT.crt
若要取得 .pfx,請使用下列命令:
openssl pkcs12 -export -out $PARENT.pfx -inkey $PARENT.key -in $PARENT.crt
備註
從 .NET 5 開始,Kestrel 除了具有密碼的 .pfx 檔案之外,還可以採用 .crt 和 PEM 編碼的.key檔案。
視主機OS而定,憑證必須受到信任。 在 Linux 主機上,憑證的「信任」方式因散佈版本而異。
如需本指南的目的,以下是使用 PowerShell 的 Windows 範例:
Import-Certificate -FilePath $certKeyPath -CertStoreLocation 'Cert:\LocalMachine\Root'
在 WSL 中使用下列命令執行範例:
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_Kestrel__Certificates__Default__Path=/https/contoso.com.crt -e ASPNETCORE_Kestrel__Certificates__Default__KeyPath=/https/contoso.com.key -v /c/path/to/certs:/https/ mcr.microsoft.com/dotnet/samples:aspnetapp
備註
在 WSL 中,磁碟區掛接路徑可能會根據設定而變更。
在 PowerShell 中執行下列命令:
docker run --rm -it -p 8000:80 -p 8001:443 -e ASPNETCORE_URLS="https://+;http://+" -e ASPNETCORE_HTTPS_PORT=8001 -e ASPNETCORE_ENVIRONMENT=Development -e ASPNETCORE_Kestrel__Certificates__Default__Path=c:\https\contoso.com.crt -e ASPNETCORE_Kestrel__Certificates__Default__KeyPath=c:\https\contoso.com.key -v c:\certs:C:\https aspnetapp:my-sample
應用程式啟動之後,瀏覽至瀏覽器中的 contoso.com:8001。
請確保主機條目已更新,使 contoso.com
能在適當的 IP 位址上回應(例如 127.0.0.1)。 如果無法辨識憑證,請確定與容器一起載入的憑證也會在主機上受到信任,而且有適當的 SAN 和 DNS 條目,contoso.com
。
收拾整理
完成測試之後,請務必清除自我簽署憑證。
Get-ChildItem $certKeyPath | Remove-Item