Create header signed request SOAP c #

1

I am consuming a webServices written in Java and in the call to the request I need a signature X509Certificate2 in the header of the request, an example of how it should look like this: External Link

I'm trying to create a WSHttpBinding , configuring it with code and setting the certificate installed on my pc through

servicio.ClientCredentials.ClientCertificate.SetCertificate.

My problem is that I created this header:

<s:Header>
  <a:Action s:mustUnderstand="1">Nombre Metodo al que llamo</a:Action>
  <a:MessageID>urn:uuid:et43534-0537-4756-8juf9-7cb56hd6e443</a:MessageID>
  <a:ReplyTo>
     <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
  </a:ReplyTo>
  <a:To s:mustUnderstand="1">DIRECCION DEL WSDL</a:To>

I've been around for quite a few days and I'm not capable.

If someone helps me, I would appreciate it.

Thanks

    
asked by rubén FR 09.04.2018 в 14:40
source

1 answer

1

Try using this:

private static void Main()
{
    using (var client = new ServiceClient())
    using (var scope = new OperationContextScope(client.InnerChannel))
    {
        MessageHeader usernameTokenHeader = MessageHeader.CreateHeader("UsernameToken",
            "http://test.com/webservices", "username");
        OperationContext.Current.OutgoingMessageHeaders.Add(usernameTokenHeader);

        MessageHeader passwordTextHeader = MessageHeader.CreateHeader("PasswordText",
            "http://test.com/webservices", "password");
        OperationContext.Current.OutgoingMessageHeaders.Add(passwordTextHeader);

        MessageHeader sessionTypeHeader = MessageHeader.CreateHeader("SessionType",
            "http://test.com/webservices", "None");
        OperationContext.Current.OutgoingMessageHeaders.Add(sessionTypeHeader);

        string result = client.GetData(1);
        Console.WriteLine(result);
    }
    Console.ReadKey();
}

The Service Trace viewer shows the following:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
        <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
        <SessionType xmlns="http://test.com/webservices">None</SessionType>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:13332/Service1.svc</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetData</Action>
    </s:Header>
</s:Envelope>

Take a look OperationContextScope for more information.

  

Other interesting information (Translate from English):

     
    
answered by 09.04.2018 в 15:59