2012-08-07 14 views
5

Tengo un servicio SOAP al que quiero conectarme. Se debe acceder a través de https y debe tener su cuerpo firmado por un certificado.WCF sobre HTTPS y firmando el cuerpo

He intentado el siguiente código:

<basicHttpBinding> 
    <binding name="P4Binding" closeTimeout="00:01:00" openTimeout="00:01:00" 
     receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" 
     bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
     maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
     messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
     useDefaultWebProxy="true"> 
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
     <security mode="TransportWithMessageCredential"> 
     <transport clientCredentialType="None" proxyCredentialType="None" 
      realm="" /> 
     <message clientCredentialType="Certificate" algorithmSuite="Default" /> 
     </security> 
    </binding> 
</basicHttpBinding> 

disposición para arriba mi cliente de la siguiente manera:

P4_ServiceReference.P4PortTypeClient client = new P4_ServiceReference.P4PortTypeClient(); 

client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None; 
client.ClientCredentials.ServiceCertificate.DefaultCertificate = new X509Certificate2(@"[..].cer"); 
client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"[..]", "somepass"); 

incluso cambió mis Reference.cs para incluir el ProtectionLevel=ProtectionLevel.Sign en el ServiceContractAttribute y la OperationContractAttribute.

Lo que sucede es que se crea el encabezado wse: security, pero el cuerpo no se está firmando. El servicio devuelve Element http://schemas.xmlsoap.org/soap/envelope/Body must be signed.

¿Qué me falta para que el cuerpo se firme correctamente?

Respuesta

8

Se corrigió utilizando customBinding en lugar de basicHttpBinding.

 //Setup custom binding with HTTPS + Body Signing + Soap1.1 
     CustomBinding binding = new CustomBinding(); 

     //HTTPS Transport 
     HttpsTransportBindingElement transport = new HttpsTransportBindingElement(); 

     //Body signing 
     AsymmetricSecurityBindingElement asec = (AsymmetricSecurityBindingElement)SecurityBindingElement.CreateMutualCertificateBindingElement(MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10); 
     asec.SetKeyDerivation(false); 
     asec.AllowInsecureTransport = true; 
     asec.IncludeTimestamp = true; 

     //Setup for SOAP 11 and UTF8 Encoding 
     TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8); 

     //Bind in order (Security layer, message layer, transport layer) 
     binding.Elements.Add(asec); 
     binding.Elements.Add(textMessageEncoding); 
     binding.Elements.Add(transport); 

Parece que TransportWithMessageCredential sólo utiliza el transporte para la seguridad y hace caso omiso de todo lo demás.

+1

Intenté firmar el cuerpo solamente, pero al usar AsymmetricSecurityBindingElement aún lo cifra (el servicio no usa https). Tengo una pregunta aquí: http://stackoverflow.com/questions/20349062/wcf-client-binding-to-sign-the-body-of-a-request-to-a-java-web-service –

+0

@MrShoubs aplique el siguiente atributo a su interfaz de servicio y el cifrado ya no ocurrirá, '[ServiceContractAttribute (ProtectionLevel = ProtectionLevel.Sign, ...)] public interface MyService {...}'. Tenga en cuenta que esto también podría realizarse desde el código con algo como 'myService.Endpoint.Contract.ProtectionLevel = ProtectionLevel.Sign'. –