2011-08-24 28 views
0

我有一個WCF服務通過https託管自簽名證書。我無法編程創建綁定:特別是端點行爲的一部分。如何以編程方式分配WCF ClientCredentials ServiceCertificate屬性?

我的服務的配置是這樣的:

<system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name=""> 
        <serviceMetadata httpsGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 

      <endpointBehaviors> 
      <behavior name="DisableServiceCertificateValidation"> 
       <clientCredentials> 
       <serviceCertificate> 
        <authentication certificateValidationMode="None" 
            revocationMode="NoCheck" /> 
       </serviceCertificate> 
       </clientCredentials> 
      </behavior> 
      </endpointBehaviors> 

     </behaviors> 
     <bindings> 
      <customBinding> 
       <binding name="ContactEmail.Web.EmailService.customBinding0"> 
        <binaryMessageEncoding /> 
        <httpsTransport/> 
       </binding> 
      </customBinding> 
     </bindings> 
     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
     <services> 
      <service name="ContactEmail.Web.EmailService"> 
       <endpoint address="https://xxxxxxxxxxxxxx/EmailService/EmailService.svc" binding="customBinding" bindingConfiguration="ContactEmail.Web.EmailService.customBinding0" contract="ContactEmail.Web.EmailService" behaviorConfiguration="DisableServiceCertificateValidation" /> 
       <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> 
      </service> 
     </services> 
    </system.serviceModel> 

當我使用「添加服務引用」功能,生成的客戶端按預期工作。鑑於我的呼叫建立一個證書驗證回調是這樣的:

System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); 

然而,而不是通過客戶端上的配置文件喂服務配置,我需要通過編程設置,因爲呼叫將成爲其中的一部分一個共享的圖書館。所以,我想通過提供自己的參數給客戶端構造像這樣做:

var myClient = new EmailServiceClient(GetBinding(), new EndpointAddress(Strings.EmailServiceEndpointAddress)); 

在GetBinding(),我創建像HttpsTransportBindingElement,BinaryMessageEncodingBindingElement和SecurityBindingElement.CreateSecureConversationBindingElement BindingElements CustomBinding(SecurityBindingElement.CreateUserNameOverTransportBindingElement( ))。

你知道我可以如何指定諸如certificateValidationMode =「None」和revocationMode =「NoCheck」之類的東西嗎?或者如果我做錯了什麼?

+1

你爲什麼要用'SecureConversation'創建綁定?您當前的配置不使用它。 –

+0

好吧,請將其作爲回答發佈,因爲刪除該設置可解決問題! –

回答

1

SecureConversation是實施WS-SecureConversation的的=>其中第一呼叫期間創建到服務(由作爲參數傳遞至結合元件創建消息安全模式驗證)的特殊安全令牌先進消息級安全並且此令牌用於保護後續消息。這種安全性也形成了一種被稱爲安全上下文或安全會話的東西

您在配置文件中的當前綁定未使用SecureConversation,因此您在代碼中定義的綁定與您的服務不兼容。

相關問題