首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 分散式開發常見錯誤 (27):安全通道無法開啟,協商失敗

     編輯:關於.NET

錯誤現象

Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint.

場景

  • 專案類型:IIS 託管 WCF 服務
  • 安全模式:訊息安全 + 自定義 X509 憑證驗證 + WSHttpBinding
  • 客戶端:ASP.NET 網站

可能原因

  1. 服務端未正確配置安全行為(憑證、驗證模式)。
  2. 服務實際未啟動(常見於同方案多專案調試時,忘記先啟動服務專案)。

解決方案

1. 檢查服務端綁定與行為配置

<wsHttpBinding>
  <binding name="MyBindingConfigration">
    <security mode="Message">
      <transport clientCredentialType="None"/>
      <message clientCredentialType="Certificate"/>
    </security>
  </binding>
</wsHttpBinding>

<serviceBehaviors>
  <behavior name="WCFService.WCFServiceBehavior">
    <serviceCredentials>
      <serviceCertificate storeName="My" x509FindType="FindBySubjectName" findValue="WCFServerPK" storeLocation="LocalMachine"/>
    </serviceCredentials>
  </behavior>
</serviceBehaviors>

確認 findValue 與本機憑證主體名稱一致,且憑證擁有私鑰、位於 LocalMachine\My 存儲區。

2. 確保服務先啟動再啟動客戶端

Visual Studio 方案包含服務與客戶端兩個專案時:

  1. 右鍵服務專案 → Debug → Start New Instance(先啟動服務)。
  2. 右鍵客戶端專案 → Debug → Start New Instance(再啟動客戶端)。

錯誤提示「與遠端端點協商失敗」實際上是服務未聽聽,導致無法進行安全握手,而非憑證本身有問題。

總結

遇到安全協商失敗,優先檢查服務是否已正確啟動並監聽端點,再驗證雙端綁定與憑證配置一致性。

參考連結

MSDN 論壇相關討論

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved