Community程式設計與開發github.com

dotnet-devcert-trust

在 Linux 上診斷與修復 .NET HTTPS 開發憑證信任問題。涵蓋從憑證產生到系統 CA 套件納入的完整生命週期,並提供 Ubuntu、Fedora、Arch 及 WSL2 的發行版特定指引。

dotnet-devcert-trust 是什麼?

此技能旨在解決 Linux 上 .NET HTTPS 開發憑證(dev cert)的信任問題,這是一個常見但令人困惑的議題。不同於 Windows 與 macOS,`dotnet dev-certs https --trust` 在 Linux 上無法自動完成信任設定,導致 .NET 應用程式、OpenSSL、curl 及瀏覽器都會拒絕該憑證。本技能提供一套 5 點的診斷程序,逐步檢查憑證是否存在、系統信任儲存區的權限、CA 套件是否納入、環境變數是否覆寫,以及符號連結的完整性。此外,也提供完整的復原指令碼,可一鍵清除陳舊檔案、重新產生憑證、匯出至系統信任儲存區,並重建 CA 套件。針對 Aspire 13.1.0 以上版本,此技能特別重要,因為其預設啟用 Redis TLS,若憑證未受信任將直接導致連線失敗。技能亦涵蓋 Ubuntu/Debian、Fedora/RHEL、Arch Linux 及 WSL2 的特定操作方式,並提供 GitHub Actions 與 Azure DevOps 的 CI/CD 自動化範例。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/dotnet-devcert-trust

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

.NET 開發憑證信任於 Linux

何時使用此技能

在以下情況使用此技能:

  • Redis TLS 連線因 UntrustedRoot 或 RemoteCertificateNameMismatch 而失敗(在 Aspire 中)
  • dotnet dev-certs https --check --trust 回傳結束代碼 7
  • HTTPS localhost 連線因憑證驗證錯誤而失敗
  • 執行 dotnet dev-certs https --clean 後需要還原信任
  • 設定新的 Linux 開發機器以進行 .NET HTTPS 開發
  • Aspire 儀表板或服務間 gRPC 呼叫因 TLS 錯誤而失敗
  • 從 Aspire < 13.1.0 升級(該版本預設未在 Redis 上使用 TLS)

問題

在 Windows 和 macOS 上,dotnet dev-certs https --trust 會自動處理所有事情——產生憑證、安裝至使用者存放區,並加入系統信任儲存區。在 Linux 上,它幾乎沒有做任何有用的事。該命令會產生憑證並放置於使用者存放區,但是:

  1. 它不會將憑證匯出至系統 CA 目錄
  2. 它不會執行 update-ca-certificates 來重建 CA 套件
  3. 它不會將憑證加入瀏覽器信任儲存區(NSS/NSSDB)
  4. --trust 旗標會靜默成功,但憑證仍不受信任

這意味著 .NET 應用程式、OpenSSL、curl 和瀏覽器都會拒絕開發憑證——即使 dotnet dev-certs https --check 回報其存在。

為什麼這在 Aspire 13.1.0+ 中會浮現

在 Aspire 13.1.0 之前,Redis 連線使用明文。從 13.1.0 開始,Aspire 預設在 Redis 上啟用 TLS。如果您的開發憑證在系統層級不受信任,Redis 連線會立即失敗,並出現:

System.Security.Authentication.AuthenticationException:
  The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot

Linux 憑證信任如何運作

了解架構可以避免盲目除錯:

┌─────────────────────────────────────────────────────┐
│ 應用程式 (.NET, curl, OpenSSL)                    │
│   讀取: /etc/ssl/certs/ca-certificates.crt         │
│         (合併的 CA 套件)                            │
└──────────────────────┬──────────────────────────────┘
                       │ 由以下工具建置
┌──────────────────────▼──────────────────────────────┐
│ update-ca-certificates                              │
│   從下列位置讀取:                                    │
│     /usr/share/ca-certificates/      (發行版 CA)   │
│     /usr/local/share/ca-certificates/ (本機 CA)    │
│   寫入至:                                           │
│     /etc/ssl/certs/ca-certificates.crt (套件)      │
│     /etc/ssl/certs/*.pem (個別符號連結)             │
└─────────────────────────────────────────────────────┘

關鍵洞察: 將 .crt 檔案放置在 /usr/local/share/ca-certificates/ 是必要但不夠的。必須執行 update-ca-certificates 來重建位於 /etc/ssl/certs/ca-certificates.crt 的合併套件。應用程式讀取的是套件,而不是個別檔案。

5 點診斷程序

按順序執行這些檢查。遇到第一個 FAIL 時停止,套用其修正後再繼續。

檢查 1:開發憑證是否存在

dotnet dev-certs https --check
echo "Exit code: $?"
結束代碼意義動作
0憑證存在於使用者存放區PASS — 繼續
非零沒有有效的開發憑證執行 dotnet dev-certs https

檢查 2:系統信任儲存區——單一憑證,正確權限

ls -la /usr/local/share/ca-certificates/ | grep -iE 'dotnet|aspnet'
結果意義
僅有 dotnet-dev-cert.crt 且權限為 -rw-r--r-- (644)PASS
多個憑證檔案、錯誤權限或陳舊的 aspnet* 檔案FAIL

來自先前階段的常見陳舊檔案:

檔案問題
aspnetcore-dev.crt通常以 0600 權限建立(update-ca-certificates 無法讀取)
aspnet/https.crt舊慣例,指紋可能與目前開發憑證不同
dotnet-dev-cert.crt 權限為 0600名稱正確但權限錯誤

修正:

# 移除所有陳舊憑證檔案
sudo rm -f /usr/local/share/ca-certificates/aspnetcore-dev.crt
sudo rm -rf /usr/local/share/ca-certificates/aspnet/

# 確保開發憑證的權限正確(如果存在)
sudo chmod 644 /usr/local/share/ca-certificates/dotnet-dev-cert.crt

檢查 3:CA 套件納入

這是最常失敗的檢查。憑證檔案存在但從未被加入套件。

openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt \
  /usr/local/share/ca-certificates/dotnet-dev-cert.crt
結果意義
dotnet-dev-cert.crt: OKPASS — 憑證已存在於合併套件中
error 20 at 0 depth lookup: unable to get local issuer certificateFAIL — 套件從未被重建
error 2 at 0 depth lookup: unable to get issuer certificateFAIL — 同樣問題,不同 OpenSSL 版本

修正:

sudo update-ca-certificates
# 預期輸出包括 "1 added" 或類似訊息

# 重新驗證
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt \
  /usr/local/share/ca-certificates/dotnet-dev-cert.crt

檢查 4:環境變數覆寫

SSL 環境變數可能會將憑證查詢重新導向至系統套件之外:

echo "SSL_CERT_DIR=${SSL_CERT_DIR:-<unset>}"
echo "SSL_CERT_FILE=${SSL_CERT_FILE:-<unset>}"
echo "DOTNET_SSL_CERT_DIR=${DOTNET_SSL_CERT_DIR:-<unset>}"
echo "DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER=${DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER:-<unset>}"
結果意義
全部 <unset>PASS
任何變數已設定FAIL — 可能重新導向憑證查詢

修正: 從您的 shell profile(~/.bashrc、~/.zshrc、~/.profile)中移除有問題的變數,然後啟動新的 shell。

檢查 5:符號連結完整性

來自已移除憑證的陳舊符號連結可能會混淆 OpenSSL:

find /etc/ssl/certs/ -xtype l 2>/dev/null | head -5
結果意義
沒有輸出PASS
列出損壞的符號連結FAIL

修正:

sudo update-ca-certificates --fresh
# 從頭重建所有符號連結

完整復原程序

當多項檢查失敗或您想要一個乾淨的開始時,執行這個完整序列:

#!/usr/bin/env bash
set -euo pipefail

echo "=== .NET 開發憑證信任復原 ==="

# 步驟 1:移除所有陳舊憑證檔案
echo "--- 移除陳舊憑證檔案 ---"
sudo rm -f /usr/local/share/ca-certificates/aspnetcore-dev.crt
sudo rm -rf /usr/local/share/ca-certificates/aspnet/
sudo rm -f /usr/local/share/ca-certificates/dotnet-dev-cert.crt

# 步驟 2:清理並重新產生開發憑證
echo "--- 重新產生開發憑證 ---"
dotnet dev-certs https --clean
dotnet dev-certs https

# 步驟 3:匯出為 PEM 並安裝至系統信任儲存區
echo "--- 安裝至系統信任儲存區 ---"
dotnet dev-certs https --export-path /tmp/dotnet-dev-cert.crt --format PEM --no-password
sudo cp /tmp/dotnet-dev-cert.crt /usr/local/share/ca-certificates/dotnet-dev-cert.crt
sudo chmod 644 /usr/local/share/ca-certificates/dotnet-dev-cert.crt
rm /tmp/dotnet-dev-cert.crt

# 步驟 4:重建 CA 套件(關鍵——最常被遺漏的步驟)
echo "--- 重建 CA 套件 ---"
sudo update-ca-certificates

# 步驟 5:驗證
echo "--- 驗證 ---"
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt \
  /usr/local/share/ca-certificates/dotnet-dev-cert.crt

echo "=== 完成!請重新啟動您的 .NET 應用程式。 ==="

將此儲存為 ~/fix-devcert.sh,並在需要時使用 bash ~/fix-devcert.sh 執行。

發行版特定注意事項

Ubuntu / Debian

上述程序是為 Ubuntu/Debian 撰寫的,可以直接使用。

  • CA 目錄: /usr/local/share/ca-certificates/
  • 套件命令: sudo update-ca-certificates
  • 套件輸出: /etc/ssl/certs/ca-certificates.crt
  • 憑證格式: 需要附檔名為 .crt 的 PEM

Fedora / RHEL / CentOS

Fedora 使用 update-ca-trust 而非 update-ca-certificates:

# 匯出憑證
dotnet dev-certs https --export-path /tmp/dotnet-dev-cert.pem --format PEM --no-password

# 安裝至 Fedora 信任儲存區(不同目錄!)
sudo cp /tmp/dotnet-dev-cert.pem /etc/pki/ca-trust/source/anchors/dotnet-dev-cert.pem
sudo chmod 644 /etc/pki/ca-trust/source/anchors/dotnet-dev-cert.pem
rm /tmp/dotnet-dev-cert.pem

# 重建信任套件
sudo update-ca-trust

# 驗證
openssl verify /etc/pki/ca-trust/source/anchors/dotnet-dev-cert.pem

主要差異:

Ubuntu/DebianFedora/RHEL
CA 目錄/usr/local/share/ca-certificates//etc/pki/ca-trust/source/anchors/
重建命令update-ca-certificatesupdate-ca-trust
套件路徑/etc/ssl/certs/ca-certificates.crt/etc/pki/tls/certs/ca-bundle.crt
副檔名.crt.pem(任何副檔名均可)

Arch Linux

Arch 使用與 Fedora 相同的 update-ca-trust 方法:

sudo cp /tmp/dotnet-dev-cert.pem /etc/ca-certificates/trust-source/anchors/dotnet-dev-cert.pem
sudo chmod 644 /etc/ca-certificates/trust-source/anchors/dotnet-dev-cert.pem
sudo update-ca-trust

WSL2

WSL2 執行真正的 Linux 核心,擁有自己的憑證儲存區——與 Windows 主機分開。標準的 Ubuntu/Debian 程序可以使用,但要注意:

  1. 共用檔案系統 (/mnt/c/)——Windows 檔案系統上的憑證檔案具有 Windows 權限,可能不是 644。請務必先複製到原生的 Linux 路徑。
  2. systemd 未執行——某些較舊的 WSL2 設定沒有 systemd,而 update-ca-certificates 的鉤子可能依賴於它。如果命令卡住,請嘗試改用 sudo dpkg-reconfigure ca-certificates。
  3. Docker Desktop 整合——如果使用 Docker Desktop 的 WSL2 後端,容器會繼承 WSL2 發行版的 CA 套件。在 WSL2 中修復信任也會為容器修復它。

Aspire 特定考量

Redis TLS (Aspire 13.1.0+)

Aspire 13.1.0 預設在 Redis 上啟用 TLS。如果您在 Redis 連線錯誤中看到:

UntrustedRoot

表示開發憑證在系統層級不受信任。請執行上述的完整復原程序。

Aspire 儀表板 HTTPS

Aspire 儀表板使用開發憑證進行 HTTPS。如果瀏覽器中顯示憑證警告,表示憑證不在瀏覽器的信任儲存區中。對於開發用途,點擊瀏覽警告是可以接受的——系統層級的信任(Redis、gRPC 等所需)才是優先事項。

ASPIRE_ALLOW_UNSECURED_TRANSPORT

設定 ASPIRE_ALLOW_UNSECURED_TRANSPORT=true 是一種權宜之計,而非解決方案。它會停用服務間通訊的 TLS,這會:

  • 掩蓋底層的信任問題
  • 不符合生產行為
  • 可能導致與生產環境中不同的錯誤

請改為修復憑證信任。

憑證生命週期

開發憑證從建立起有效期限為 1 年。當它過期時:

  1. dotnet dev-certs https --check 會回報沒有有效的憑證
  2. 執行完整復原程序以產生新的憑證
  3. /usr/local/share/ca-certificates/ 中的舊憑證檔案將被取代
  4. update-ca-certificates 會將套件中的舊憑證換成新的

無需重新啟動系統。 應用程式會在下一次 TLS 握手時(重新啟動您的應用程式)取得新的套件。

自動化:CI/CD 管線

在 Linux 執行器上的 CI/CD 中,很少需要開發憑證(您通常會針對真實憑證進行測試,或在測試框架中停用 TLS 驗證)。但是,如果您的整合測試需要受信任的開發憑證:

GitHub Actions

- name: Trust .NET Dev Certificate
  run: |
    dotnet dev-certs https
    dotnet dev-certs https --export-path /tmp/dotnet-dev-cert.crt --format PEM --no-password
    sudo cp /tmp/dotnet-dev-cert.crt /usr/local/share/ca-certificates/dotnet-dev-cert.crt
    sudo chmod 644 /usr/local/share/ca-certificates/dotnet-dev-cert.crt
    rm /tmp/dotnet-dev-cert.crt
    sudo update-ca-certificates

Azure DevOps

- script: |
    dotnet dev-certs https
    dotnet dev-certs https --export-path /tmp/dotnet-dev-cert.crt --format PEM --no-password
    sudo cp /tmp/dotnet-dev-cert.crt /usr/local/share/ca-certificates/dotnet-dev-cert.crt
    sudo chmod 644 /usr/local/share/ca-certificates/dotnet-dev-cert.crt
    rm /tmp/dotnet-dev-cert.crt
    sudo update-ca-certificates
  displayName: 'Trust .NET Dev Certificate'

常見陷阱

1. 憑證已放置但套件從未重建

症狀: 憑證檔案存在於 /usr/local/share/ca-certificates/ 但 openssl verify 失敗。

原因: 放置檔案後從未執行 update-ca-certificates。

修正: sudo update-ca-certificates

這是單一最常見的錯誤。CA 目錄是套件產生過程的輸入,而不是套件本身。

2. 陳舊的憑證檔案具有錯誤的權限

症狀: update-ca-certificates 執行但回報 0 added。

原因: 權限為 0600 的憑證檔案無法被 update-ca-certificates 讀取(它雖然以 root 執行,但透過一個可能檢查全域可讀性的程序來讀取檔案)。檔案必須是 644。

修正: sudo chmod 644 /usr/local/share/ca-certificates/*.crt

3. 來自不同階段的多個憑證檔案

症狀: update-ca-certificates 加入了多個憑證,但應用程式仍然失敗。

原因: 來自先前 dotnet dev-certs https --clean / 重新產生週期的舊憑證檔案仍留在 CA 目錄中。舊憑證的指紋與目前的開發憑證不符。

修正: 移除所有 dotnet* 和 aspnet* 檔案,然後重新匯出目前的憑證。

4. 清理/重新產生後的指紋不符

症狀: openssl verify 通過,但 .NET 仍回報 UntrustedRoot。

原因: /usr/local/share/ca-certificates/ 中的憑證是從先前的開發憑證匯出的。在 dotnet dev-certs https --clean && dotnet dev-certs https 之後,會產生一個新的憑證

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

akka-hosting-actor-patterns

Patterns for building entity actors with Akka.Hosting - GenericChildPerEntityParent, message extractors, cluster sharding abstraction, akka-reminders, and ITimeProvider. Supports both local testing and clustered production modes.

akka-net-aspire-configuration

Configure Akka.NET with .NET Aspire for local development and production deployments. Covers actor system setup, clustering, persistence, Akka.Management integration, and Aspire orchestration patterns.

akka-net-best-practices

Critical Akka.NET best practices including EventStream vs DistributedPubSub, supervision strategies, error handling, Props vs DependencyResolver, work distribution patterns, and cluster/local mode abstractions for testability.

akka-net-management

Akka.Management for cluster bootstrapping, service discovery (Kubernetes, Azure, Config), health checks, and dynamic cluster formation without static seed nodes.

akka-net-testing-patterns

Write unit and integration tests for Akka.NET actors using modern Akka.Hosting.TestKit patterns. Covers dependency injection, TestProbes, persistence testing, and actor interaction verification. Includes guidance on when to use traditional TestKit.

api-design

Design stable, compatible public APIs using extend-only design principles. Manage API compatibility, wire compatibility, and versioning for NuGet packages and distributed systems.

aspire-configuration

Configure Aspire AppHost to emit explicit app config via environment variables; keep app code free of Aspire clients and service discovery.

aspire-integration-testing

Write integration tests using .NET Aspire

aspire-service-defaults

Create a shared ServiceDefaults project for Aspire applications. Centralizes OpenTelemetry, health checks, resilience, and service discovery configuration across all services.

crap-analysis

Analyze code coverage and CRAP (Change Risk Anti-Patterns) scores to identify high-risk code. Use OpenCover format with ReportGenerator for Risk Hotspots showing cyclomatic complexity and untested code paths.

csharp-concurrency-patterns

Choosing the right concurrency abstraction in .NET - from async/await for I/O to Channels for producer/consumer to Akka.NET for stateful entity management. Avoid locks and manual synchronization unless absolutely necessary.

database-performance

Database access patterns for performance. Separate read/write models, avoid N+1 queries, use AsNoTracking, apply row limits, and never do application-side joins. Works with EF Core and Dapper.

dependency-injection-patterns

Organize DI registrations using IServiceCollection extension methods. Group related services into composable Add* methods for clean Program.cs and reusable configuration in tests.

dotnet-local-tools

Managing local .NET tools with dotnet-tools.json for consistent tooling across development environments and CI/CD pipelines.

dotnet-project-structure

Modern .NET project structure including .slnx solution format, Directory.Build.props, central package management, SourceLink, version management with RELEASE_NOTES.md, and SDK pinning with global.json.

dotnet-slopwatch

Use Slopwatch to detect LLM reward hacking in .NET code changes. Run after every code modification to catch disabled tests, suppressed warnings, empty catch blocks, and other shortcuts that mask real problems.

efcore-patterns

Entity Framework Core best practices including NoTracking by default, query splitting for navigation collections, migration management, dedicated migration services, and common pitfalls to avoid.

ilspy-decompile

Understand implementation details of .NET code by decompiling assemblies. Use when you want to see how a .NET API works internally, inspect NuGet package source, view framework implementation, or understand compiled .NET binaries.

mailpit-integration

Test email sending locally using Mailpit with .NET Aspire. Captures all outgoing emails without sending them. View rendered HTML, inspect headers, and verify delivery in integration tests.

marketplace-publishing

Workflow for publishing skills and agents to the dotnet-skills Claude Code marketplace. Covers adding new content, updating plugin.json, validation, and release tagging.

相關技能