如何在c#的客户端请求中插入此soap头

时间:2016-06-07 07:41:41

标签: c# web-services wcf soap wsse

我必须为soap web服务插入此标头客户端请求。我该怎么做?

 <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>

客户端代码:我使用wshttpbinding进行soap web服务。所以我必须在上面发送带有soap header的请求。我该怎么办?

    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);

        }
    }

2 个答案:

答案 0 :(得分:0)

您可以使用属性[MessageHeader]来定义标头和属性[MessageBodyMember]以定义SOAP消息的主体。在YourClass修饰的WCF服务方法定义中添加[OperationContract]作为参数。然后在您的客户端中,使用YourClass作为参数调用此方法。

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

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

添加邮件标头的另一种方法:

在代理类中:

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

在服务类:

OperationContext context = OperationContext.Current;
var HeaderValue = context.IncomingMessageHeaders.GetHeader<string>("String", "System");

答案 1 :(得分:0)

我完成了这个链接:)

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

我使用custombinding而不是wshttpbinding