0

I have to insert this header client side request for soap web service.How can I do ?

 <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
    <wsse:Security soap:mustUnderstand="true" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <wsse:UsernameToken wsu:Id="UsernameToken-A116E02D59482B66FF14652216221791"><wsse:Username>test</wsse:Username>
    <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test2</wsse:Password>
    <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">7og8drnSMbkytwYPlDrCCg==</wsse:Nonce><wsu:Created>2016-06-06T14:00:22.177Z</wsu:Created>
    </wsse:UsernameToken></wsse:Security>
    <wsa:Action soap:mustUnderstand="1">http://tempuri.org/IQuantityService/LoadDebitAmount</wsa:Action></soap:Header>

Client side code :I using wshttpbinding for soap web service.So I have to send request with soap header at above.How can I do ?

    private void btnLoadAmountTest_Click(object sender, EventArgs e)
    {
        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

        try
        {

            WSHttpBinding userNameBinding = new WSHttpBinding();
            userNameBinding.Security.Mode = SecurityMode.Transport;
            userNameBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

            EndpointAddress endpointAddress = new EndpointAddress(portB.ToString());

            QuantityServiceClient client = new ServiceReferenceLoadQuantity.QuantityServiceClient(userNameBinding, endpointAddress);
                client.ChannelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>();
            client.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentials());
            client.ClientCredentials.UserName.UserName = txtUser2.Text;
            client.ClientCredentials.UserName.Password = txtPsw2.Text;


            //using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            //{

            //    var httpRequestProperty = new HttpRequestMessageProperty();
            //    httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] =
            //      "Basic " +
            //      Convert.ToBase64String(Encoding.ASCII.GetBytes(
            //             client.ClientCredentials.UserName.UserName + ":" +
            //             client.ClientCredentials.UserName.Password));

            //    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] =
            //                httpRequestProperty;


            //    // client.ClientCredentials.SupportInteractive = true;





            //using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            //{

            //    OperationContext.Current.OutgoingMessageHeaders.Add(
            //        new SecurityHeader("UsernameToken-49", client.ClientCredentials.UserName.UserName, client.ClientCredentials.UserName.Password));
              OperationResult result = new OperationResult();


                result = client.LoadDebitAmount(txtLoadAmountCust.Text, txtLoadAmountDep.Text, txtLoadAmountPlate.Text, txtLoadAmountFType.Text, txtLoadAmountFCode.Text, txtLoadAmountAmt.Text, txtLoadAmountInvNo.Text, txtLoadAmountInvDate.Text);

                txtLoadAmountOut.Text = result.ReturnCode.ToString() + " " + result.ReturnMessage.ToString();
                // ServiceReference1.GlobalWeatherSoapClient soap = new ServiceReference1.GlobalWeatherSoapClient();

                //txtLoadQuantityOutText.Text= soap.GetWeather("Istanbul","Turkey");
                client.Close();
            //}
            //}
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);

        }
    }
Sezer Erdogan
  • 167
  • 1
  • 9
  • 34

2 Answers2

0

You can use attribute [MessageHeader] to define headers and attribute [MessageBodyMember] to define body of your SOAP message. Add YourClass as argument in your WCF service method definition decorated with [OperationContract]. Then in your client just call this method with YourClass as argument.

[MessageContract(IsWrapped = false)]
    public class YourClass
    {
        [MessageHeader]
        public string HeaderName;

        [MessageBodyMember(Order = 1)]
        public string BodyVariable;
    }

Another way to add message headers:

In proxy class:

  MessageHeader<string> header = new MessageHeader<string>("YourHeaderValue");
    OperationContextScope contextScope = new OperationContextScope(InnerChannel);                
OperationContext.Current.OutgoingMessageHeaders.Add(header.GetUntypedHeader("String","System"));

In Service class:

OperationContext context = OperationContext.Current;
var HeaderValue = context.IncomingMessageHeaders.GetHeader<string>("String", "System");
Nick
  • 423
  • 4
  • 19
  • I really recommend to create boilerplate Visual Studio WCF solution and try to do it firstly there. This is a good basic example of WCF services usage. – Nick Jun 07 '16 at 08:24
  • Nick I already have web service.But I write one client side project for testing.Normally I using soapui.But I write one test tool.So I have to send webservice this request at client side. – Sezer Erdogan Jun 07 '16 at 08:36
  • Do you have any problems with changing service contract? I will edit my answer. – Nick Jun 07 '16 at 08:45
  • My service contract already operationcontract.I need client side this changing.I will not touch my web service.Because it is working fine when I tested soapui.But I dont use soap ui.So I write my test tool.So I have to send request to service and service must be except my soap message.So I have to add secure soap header message in my request. – Sezer Erdogan Jun 07 '16 at 08:55
  • Nick I will try your recommendations – Sezer Erdogan Jun 07 '16 at 09:06
  • Nick How can I prepared header value at the top ? do you know ? – Sezer Erdogan Jun 07 '16 at 09:12
  • I was sending only simple strings. – Nick Jun 07 '16 at 09:29
0

I done with this link :)

https://weblog.west-wind.com/posts/2012/Nov/24/WCF-WSSecurity-and-WSE-Nonce-Authentication

I use custombinding instead of wshttpbinding

Sezer Erdogan
  • 167
  • 1
  • 9
  • 34