編譯 DotNetOpenAuth 5.1 源碼遭遇強名稱驗證失敗,完整記錄 `sn.exe` 金鑰生成、`msbuild` 強簽編譯、GAC 註冊失敗排查、延遲簽章陷阱的完整過程。
msbuild tools/drop.proj
輸出 `drops\v4.5\Debug\DotNetOpenAuth.dll` (已合併)
Could not load file or assembly 'DotNetOpenAuth' or one of its dependencies.
Strong name signature could not be verified. The assembly may have been tampered with,
or it was delay signed but not fully signed with the correct private key.
(Exception from HRESULT: 0x80131045)
sn -k mykeyfile.pfx # 生成公開/私鑰對到 .pfx
sn -i mykeyfile.pfx mycontainer # 安裝至強名稱金鑰容器
sn -p mykeyfile.pfx mykeyfile.pub # 導出公開金鑰
sn -q -t mykeyfile.pub # 顯示公開金鑰 Token
msbuild /p:KeyPairContainer=mycontainer,PublicKeyFile="C:\Full\Path\mykeyfile.pub" tools/drop.proj
**頓悟**:強名稱驗證**僅在組件安裝至 GAC 或啟用強名稱驗證時生效**。專案參考本地 DLL 不觸發驗證,除非專案設定 `SignAssembly=true` 並要求強名稱。
gacutil /i DotNetOpenAuth.dll
報錯:`Failure adding assembly to the cache: Strong name signature could not be verified. Was the assembly built delay-signed?`
| 概念 | 說明 |
|---|---|
| 強名稱 | 組件身分 = 簡單名稱 + 版本 + 文化特性 + 公開金鑰 Token |
| sn.exe | Strong Name 工具:生成金鑰、管理容器、驗證、重新簽章 |
| 延遲簽章 | 僅預留公開金鑰空間,建置後再用私鑰簽章 (適合開發/測試環境) |
| GAC | 全域組件快取,**強制要求強名稱**,驗證簽章有效性 |
| ILMerge | 合併組件工具,**必須顯式指定私鑰** 才能產出有效強簽組件 |
| 驗證時機 | 安裝 GAC、啟用 `bypassTrustedAppStrongNames=false`、強名稱繫結重導向時 |
強名稱 ≠ 安全性。它解決**組件身分唯一性**與**版本衝突**,不防範惡意竄改 (需 Authenticode 代碼簽章)。現代 .NET Core/.NET 5+ 已淡化強名稱,改用套件管理 (NuGet) 解決版本問題。