在 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憑證?

您可以透過執行指令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 中將環境配置為「生產」環境,請在 Docker 執行指令中設定環境變數ENVIRONMENT=Production

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

在 Docker 命令中,請將<IRONSECUREDOC_LICENSE_KEY><CERTIFICATE_PASSWORD>等佔位符替換為您實際的 IronSecureDoc 許可證密鑰和證書密碼,以確保其正常運作。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
版本: 2024.10 剛剛發布