在使用 Transport 安全模式與 WSHttpBinding,並將客戶端認證類型設為 Windows (clientCredentialType="Windows") 時,即便已安裝憑證並添加服務引用,啟動服務宿主後仍會拋出以下異常:
An error occurred while making the HTTP request to https://localhost:9001/WCFService. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
中文意為:伺服器憑證在 HTTPS 情況下未與 HTTP.SYS 正確配置,或客戶端與服務端的安全繫結不匹配。
開啟 Visual Studio 開發人員命令提示字元,執行:
makecert -r -pe -n "CN=FrankWCFServer" -ss My -sky exchange
參數說明:
-r:建立自我簽署憑證-pe:私鑰可匯出-n "CN=FrankWCFServer":主體名稱-ss My:存入「個人」憑證存儲區-sky exchange:金鑰交換用途在 <serviceBehaviors> 中添加 <serviceCertificate> 節點:
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceCredentials>
<serviceCertificate x509FindType="FindBySubjectName" findValue="WCFHTTPS" storeLocation="CurrentUser"/>
</serviceCredentials>
</behavior>
此步驟為關鍵,必須將憑證指紋綁定至具體 IP:Port。
在瀏覽器「內容 → 憑證 → 詳細資料 → 指紋」中複製值,移除空格。例如:0307d727a369378d077809cf0ed617eec90a0e2f
# 查詢現有配置
httpcfg query ssl
# 註冊憑證到連接埠 9001
httpcfg set ssl -i 0.0.0.0:9001 -h 0307d727a369378d077809cf0ed617eec90a0e2f
# 刪除配置
httpcfg delete ssl -i 0.0.0.0:9001 -h 0307d727a369378d077809cf0ed617eec90a0e2f
# 查詢現有配置
netsh http show sslcert
# 註冊憑證
netsh http add sslcert ipport=0.0.0.0:9001 certhash=0307d727a369378d077809cf0ed617eec90a0e2f appid={11111111-2222-3333-4444-555555555555}
# 刪除配置
netsh http delete sslcert ipport=0.0.0.0:9001
注意:appid 可為任意 GUID,用於標識應用程式身分。
完成上述配置後,重新啟動服務宿主,HTTPS 端點應能正常監聽,客戶端可成功建立安全通道並呼叫服務。

WCF Transport 安全模式下,除了在配置文件指定服務憑證外,務必將憑證註冊到 HTTP.SYS 的對應連接埠,否則會出現「server certificate is not configured properly with HTTP.SYS」錯誤。此步驟常被文獻忽略,實為 HTTPS 自託管服務部署的關鍵環節。