2017-06-01 86 views
0

這是我第一次創建WCF服務。我需要使用HTTPS,因爲我將使用MembershipBinding。我已採取的步驟是:WCF服務自簽名證書在iis的本地主機上無效

  1. 使用makecert.exe應用程序創建證書頒發機構 - 從此我創建了服務器證書和客戶端證書。
  2. 將證書頒發機構添加到Microsoft管理控制檯中的受信任根證書頒發機構。
  3. 將客戶端和服務器證書添加到Microsoft管理控制檯中的我的個人證書。
  4. 使用服務器證書在IIS中爲服務創建一個https綁定。
  5. 在服務器證書上爲應用程序池設置適當的權限。
  6. 在web.config中的serviceBehaviours節點內定義服務證書。

現在我使用的是WCF測試客戶端測試服務,但我得到的消息:

Error: Cannot obtain Metadata from https://localhost:444/Service.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: https://localhost:444/Service.svc Metadata contains a reference that cannot be resolved: ' https://localhost:444/Service.svc '. Could not establish trust relationship for the SSL/TLS secure channel with authority 'localhost:444'. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. The remote certificate is invalid according to the validation procedure.HTTP GET Error URI: https://localhost:444/Service.svc There was an error downloading ' https://localhost:444/Service.svc '. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. The remote certificate is invalid according to the validation procedure.

錯誤表明,有信任證書的問題,但我一直信賴使用的證書頒發機構創建它,所以我不知道如何解決它。當我使用http時,服務工作正常。

在此先感謝。

回答

0

由於您的證書是自簽名的,你需要一個黑客添加到您的客戶端調用:

using (MyWCFServiceClient client = new MyWCFServiceClient()) 
{ 

#if DEBUG 
    ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificatesCallback; 
#endif 

    client.MyCall(); 
} 

而且定義TrustAllCertificatesCallback:

internal static bool TrustAllCertificatesCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors) 
{ 
    bool isValid = true; 
    // TODO logic to check your self-signed certifiacte 

    return isValid; 
} 

的TrustAllCertificatesCallback回調應停用有關你的生產環境。