在 C# 中使用 HTTPS,方法是使用憑證執行容器。

This article was translated from English: Does it need improvement?
Translated
View the article in English

如果需要使用憑證執行容器,請依照下列步驟產生憑證並設定本機:

使用憑證運行 IronSecureDoc 容器

1.生成證書

   # Generates a new HTTPS certificate for use with ASP.NET Core.
   # Saves the certificate at the specified path and protects it with a password.
   dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\ironsecuredoc.pfx -p <CREDENTIAL_PLACEHOLDER>

   # Trusts the generated certificate on your local machine.
   dotnet dev-certs https --trust
   # Generates a new HTTPS certificate for use with ASP.NET Core.
   # Saves the certificate at the specified path and protects it with a password.
   dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\ironsecuredoc.pfx -p <CREDENTIAL_PLACEHOLDER>

   # Trusts the generated certificate on your local machine.
   dotnet dev-certs https --trust
SHELL

代替<CREDENTIAL_PLACEHOLDER>使用密碼。

2.運行 Docker 容器

   # Runs the IronSecureDoc Docker container with both HTTP and HTTPS ports exposed.
   docker container run -d \
     -p 8081:8081 -p 8080:8080 \ # Maps container ports to host ports for HTTP and HTTPS.
     -e HTTP_PORTS=8080 \ # Sets the HTTP port for the application.
     -e HTTPS_PORTS=8081 \ # Sets the HTTPS port for the application.
     -e IronSecureDoc_LicenseKey=<IRONSECUREDOC_LICENSE_KEY> \ # Provides the IronSecureDoc license key.
     -e ENVIRONMENT=Production \ # Configures the environment as Production.
     -v %USERPROFILE%\.aspnet\https:/https:ro \ # Mounts the directory to the container as read-only.
     -e CERTIFICATE_PATH=%USERPROFILE%\.aspnet\https\ironsecuredoc.pfx \ # Specifies the path to the certificate in the container.
     -e CERTIFICATE_PASSWORD=<CERTIFICATE_PASSWORD> \ # Password for the certificate.
     ironsoftwareofficial/ironsecuredoc:latest # Specifies the Docker image to use.
   # Runs the IronSecureDoc Docker container with both HTTP and HTTPS ports exposed.
   docker container run -d \
     -p 8081:8081 -p 8080:8080 \ # Maps container ports to host ports for HTTP and HTTPS.
     -e HTTP_PORTS=8080 \ # Sets the HTTP port for the application.
     -e HTTPS_PORTS=8081 \ # Sets the HTTPS port for the application.
     -e IronSecureDoc_LicenseKey=<IRONSECUREDOC_LICENSE_KEY> \ # Provides the IronSecureDoc license key.
     -e ENVIRONMENT=Production \ # Configures the environment as Production.
     -v %USERPROFILE%\.aspnet\https:/https:ro \ # Mounts the directory to the container as read-only.
     -e CERTIFICATE_PATH=%USERPROFILE%\.aspnet\https\ironsecuredoc.pfx \ # Specifies the path to the certificate in the container.
     -e CERTIFICATE_PASSWORD=<CERTIFICATE_PASSWORD> \ # Password for the certificate.
     ironsoftwareofficial/ironsecuredoc:latest # Specifies the Docker image to use.
SHELL

代替<IRONSECUREDOC_LICENSE_KEY><CERTIFICATE_PASSWORD>分別使用您的 IronSecureDoc 授權金鑰和憑證密碼。

常見問題解答

為什麼 HTTPS 對文件安全性很重要?

HTTPS 對於文件的安全性至關重要,因為 HTTPS 會加密傳輸中的資料,以確保敏感資訊在傳輸過程中保持機密且不受攔截。

如何為我的應用程式建立 HTTPS 證書?

您可以透過執行指令 dotnet dev-certs https -ep %USERPROFILE%\.aspnet\https\ironsecuredoc.pfx -p <CREDENTIAL_PLACEHOLDER>來建立 HTTPS 證書,此指令會產生新的證書,供 ASP.NET Core 使用。

如何確保我的 HTTPS 憑證在本機上受到信任?

若要信任本機上的 HTTPS 證書,請使用 dotnet dev-certs https --trust。此指令會設定您的系統信任所產生的憑證。

執行支援 HTTPS 的 Docker 容器的流程為何?

若要執行支援 HTTPS 的 Docker 容器,請開啟 HTTP 和 HTTPS 連接埠,設定必要的環境變數,例如 IronSecureDoc_LicenseKeyCERTIFICATE_PASSWORD ,並將證書目錄掛載為唯讀。

在 Docker 設定中,哪些連接埠應該設定為 HTTPS?

若要在 Docker 設定中進行 HTTPS 配置,請透過將連接埠 8081 開放為 HTTPS,將連接埠 8080 開放為 HTTP,將容器連接埠對應到主機連接埠。

如何將證書密碼傳給 Docker 容器?

在執行 Docker run 指令時,使用環境變數 CERTIFICATE_PASSWORD 將證書密碼傳送給 Docker 容器。

在 Docker 中將環境設定為「生產」需要哪些步驟?

若要在 Docker 中將環境設定為「Production」,請在 Docker 執行指令中設定環境變數 ENVIRONMENT=Production

在 Docker 指令中應該如何處理占位符?

在 Docker 指令中,請將占位符如 <IRONSECUREDOC_LICENSE_KEY><CERTIFICATE_PASSWORD> 替換為您實際的 IronSecureDoc 授權金鑰和證書密碼,以獲得正確的功能。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Version: 2024.10 剛發表