我有一個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」之類的東西嗎?或者如果我做錯了什麼?
你爲什麼要用'SecureConversation'創建綁定?您當前的配置不使用它。 –
好吧,請將其作爲回答發佈,因爲刪除該設置可解決問題! –