2011-12-18 3 views
1

Я пытаюсь реализовать пользовательскую привязку WS с защитой от сжатия и сообщений с ClientCredentialType, установленным в «None». Служба настроена и работает успешно. Мне также удалось настроить клиента и запустить его успешно. Тем не менее, мне нужно настроить программу программно, поэтому, когда я пытаюсь перевести конфигурацию клиента в код, я получаю сообщение об ошибке «Сертификат службы не указан для« ххх »цели. Укажите сертификат службы в ClientCredentials. ' Я использую автогенерированный прокси-клиент, и я следил за рекомендациями, чтобы переопределить конструктор клиента и указать сертификат службы CertificateValidationMode непосредственно на ClientCredentials или в поведении клиента, но все равно не повезло.Ошибка привязки пользовательской WS: сертификат сервиса не указан для целевого «xxx». Укажите сертификат службы в ClientCredentials

Буду признателен за любую помощь в разрешении этого вопроса. Для справки, я включаю ниже конфигурацию и ее перевод кода.

конфигурация

Клиента:

<system.serviceModel> 
<bindings> 
    <customBinding> 
    <binding name="customWSBinding" sendTimeout="00:15:00"> 
     <security authenticationMode="SecureConversation" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"> 
     <secureConversationBootstrap authenticationMode="AnonymousForSslNegotiated" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" /> 
     </security> 
     <gzipMessageEncoding innerMessageEncoding="textMessageEncoding"/> 
     <httpTransport hostNameComparisonMode="StrongWildcard" manualAddressing="False" 
         maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 
         authenticationScheme="Anonymous" bypassProxyOnLocal="False" realm="" useDefaultWebProxy="True"/> 
    </binding> 
    </customBinding> 
</bindings> 
<client> 
    <endpoint address="" 
    binding="customBinding" 
    bindingConfiguration="customWSBinding" 
    behaviorConfiguration="ClientBehavior" 
    contract="IService" 
    name="ServiceEndpoint"> 
    <identity> 
     <dns value="contoso.com"/> 
    </identity> 
    </endpoint> 
</client> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="ClientBehavior"> 
     <clientCredentials> 
     <serviceCertificate> 
      <authentication certificateValidationMode="None"/> 
     </serviceCertificate> 
     </clientCredentials> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
</system.serviceModel> 

Эквивалентный код:

SecurityBindingElement securityElement = SecurityBindingElement.CreateSecureConversationBindingElement(SecurityBindingElement.CreateAnonymousForCertificateBindingElement()); 
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10; 

GZipMessageEncodingBindingElement encodingElement = new GZipMessageEncodingBindingElement(); 
TextMessageEncodingBindingElement txtMsgBE = new TextMessageEncodingBindingElement(); 
encodingElement.InnerMessageEncodingBindingElement = txtMsgBE; 

HttpTransportBindingElement httpTransportElement = new HttpTransportBindingElement(); 
httpTransportElement.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard; 
httpTransportElement.ManualAddressing = false; 
httpTransportElement.MaxReceivedMessageSize = Int32.MaxValue; 
httpTransportElement.MaxBufferSize = Int32.MaxValue; 
httpTransportElement.MaxBufferPoolSize = Int32.MaxValue; 
httpTransportElement.AuthenticationScheme = AuthenticationSchemes.Anonymous; 
httpTransportElement.BypassProxyOnLocal = false; 
httpTransportElement.UseDefaultWebProxy = true; 

System.ServiceModel.Channels.Binding binding = new CustomBinding(securityElement, encodingElement, httpTransportElement); 
binding.SendTimeout = TimeSpan.FromMinutes(15); 

EndpointAddress address = new EndpointAddress(new Uri(svcURL), EndpointIdentity.CreateDnsIdentity("contoso.com")); 

ServiceClient svcClient = new ServiceClient(binding, address); 

Переопределенный прокси-клиент:

public ServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) 
:base (binding, remoteAddress) 
{ 
    System.ServiceModel.Description.ClientCredentials cc = new System.ServiceModel.Description.ClientCredentials(); 
    cc.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; 

    base.Endpoint.Behaviors.RemoveAt(1); 
    base.Endpoint.Behaviors.Add(cc); 
} 
+0

Возможный дубликат http://stackoverflow.com/questions/4441072/the-service-certificate-is-not-provided-for-target-specify-a-service-certi –

ответ

0

CreateAnonymousForCertificateBindingElement() метод обеспечивает обязательный элемент для анонимного клиента аутентификации и сертификата на основе аутентификация службы. Следовательно, если запрашивается сертификат службы.

Смежные вопросы