简体   繁体   中英

WCF C# Soap service not rendering correctly

I am trying to make a SOAP call to a webservice.

According to the manual the SOAP call should look like this:

<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-secext-1.0.xsd"

xmlns:wsa="http://www.w3.org/2005/08/addressing"


xmlns:vh="http://vedaxml.com/soap/header/v-header-v1-9.xsd"


xmlns:idm="http://vedaxml.com/vxml2/idmatrix-v4-0.xsd">

<soapenv:Header>



<wsse:Security>
 <wsse:UsernameToken>
  <wsse:Username>****</wsse:Username>
  <wsse:Password>****</wsse:Password>
 </wsse:UsernameToken>
 </wsse:Security>
 <wsa:ReplyTo>
 <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous
</wsa:Address>
 </wsa:ReplyTo>
 <wsa:To>http://vedaxml.com/sys2/idmatrix-v4</wsa:To>
 <wsa:Action>http://vedaxml.com/idmatrix/VerifyIdentity</wsa:Action>
 <wsa:MessageID>Quick_Request_1</wsa:MessageID>
</soapenv:Header>
 <soapenv:Body>
  <idm:request client-reference="Quick Connect Ref" 
                            reason-for-enquiry="Quick Connect">  
<idm:individual-name>
   <idm:family-name>Potter</idm:family-name>
   <idm:first-given-name>Harry</idm:first-given-name>
   <idm:other-given-name>James</idm:other-given-name>
   </idm:individual-name>
   <idm:date-of-birth>1980-07-31</idm:date-of-birth>
   <idm:current-address> 
<idm:property>Potter Manor</idm:property>
    <idm:unit-number>3</idm:unit-number>
    <idm:street-number>4</idm:street-number>
    <idm:street-name>Privet</idm:street-name>
    <idm:street-type>Drive</idm:street-type>
    <idm:suburb>Little Whinging</idm:suburb> 
    <idm:state>NSW</idm:state> 
    <idm:postcode>2999</idm:postcode>
   </idm:current-address>
  </idm:request>
 </soapenv:Body> 
</soapenv:Envelope> 

The company has provided me with a WSDL file. Which I have imported into C# as both a Service Reference and a Web Reference (Because I couldnt get service reference working)

First of all I tried a Service Reference, no bindings were created in web.config when I imported the WSDL file, So I programmatically added the bindings. This is my code:

BasicHttpsBinding binding = new BasicHttpsBinding(securityMode: BasicHttpsSecurityMode.TransportWithMessageCredential);
            EndpointAddress address = new EndpointAddress("https://ctaau.vedaxml.com/cta/sys2/idmatrix-v4");

            ServiceReference1.identitydetails idDetails = new ServiceReference1.identitydetails();               
            ServiceReference1.IdMatrixPortTypeClient client = new ServiceReference1.IdMatrixPortTypeClient(binding, address);
            ServiceReference1.request request = new ServiceReference1.request();
            request.individualname = new ServiceReference1.individualnameType();
            request.individualname.familyname = "Doe";
            request.individualname.firstgivenname = "John";
            request.dateofbirth = new DateTime(1982, 6, 30);

            client.ClientCredentials.UserName.Password = "myPassword";
            client.ClientCredentials.UserName.UserName = "myUsername";
            var response = client.IdMatrixOperation(request);

The resposne failed, so I checked the POST with Fiddler. It was coming up completely different to the required post.

Fiddler showed me:

POST https://ctaau.vedaxml.com/cta/sys2/idmatrix-v4 HTTP/1.1 Content-Type: text/xml; charset=utf-8 SOAPAction: " http://vedaxml.com/idmatrix/VerifyIdentity " Host: ctaau.vedaxml.com Content-Length: 1216 Expect: 100-continue Accept-Encoding: gzip, deflate Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo/2Z+y84o0RBobFBWyoF5bwAAAAAMq+GwwIcokm+lVAU/kPq4mJScS+4yh9ElAQSyM2iUqMACQAA</VsDebuggerCausalityData>
        <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2016-03-23T01:23:53.921Z</u:Created>
                <u:Expires>2016-03-23T01:28:53.921Z</u:Expires>
            </u:Timestamp>
            <o:UsernameToken u:Id="uuid-f3068a37-421c-4b81-93c6-ef622def918a-1">
                <o:Username>myUsername</o:Username>
                <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">myPassword</o:Password>
            </o:UsernameToken>
        </o:Security>
    </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <request xmlns="http://vedaxml.com/vxml2/idmatrix-v4-0.xsd">
            <individual-name>
                <family-name>Doe</family-name>
                <first-given-name>John</first-given-name>
            </individual-name>
        </request>
    </s:Body>
</s:Envelope>

As you can see the WSSE name space is incorrect. I dont have WS-Addressing and The Prefix's are all wrong?

So then I moved to A Web Reference. I needed to get WS-Secrutiy working, so I changed the client protocol to WebServicesClientProtocol.

This is my code:

  WebReference.idmatrix idMatrix = new WebReference.idmatrix();
            WebReference.request request = new WebReference.request();
            request.individualname = new WebReference.individualnameType();
            request.individualname.familyname = "Potter";
            request.individualname.othergivenname = names;
            request.individualname.firstgivenname = "Harry";
            request.dateofbirth = new DateTime(1980, 7, 31);


            UsernameToken userToken = new UsernameToken("myusername", "mypassword", PasswordOption.SendPlainText);

            SoapContext requestContext = idMatrix.RequestSoapContext;
            requestContext.Security.Tokens.Add(userToken);

            var test = idMatrix.IdMatrixOperation(request);

The Post in fidder looks like:

POST https://vedaxml.com/sys2/idmatrix-v4 HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)
VsDebuggerCausalityData: uIDPo+bosvSda9ZIvNIXrG1tEbIAAAAAH4GKWz0+uU2A4pOErBrkw2fytXzxQh9Jp4TpqUiqXHoACQAA
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://vedaxml.com/idmatrix/VerifyIdentity"
Host: vedaxml.com
Content-Length: 1768
Expect: 100-continue
Connection: Keep-Alive

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" 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">
    <soap:Header>
        <wsa:Action>http://vedaxml.com/idmatrix/VerifyIdentity</wsa:Action>
        <wsa:MessageID>uuid:d1a0cf65-cee3-467d-9f98-b72cbd3d4e5a</wsa:MessageID>
        <wsa:ReplyTo>
            <wsa:Address>http://schemas.xmlsoap.org/ws/2004/03/addressing/role/anonymous</wsa:Address>
        </wsa:ReplyTo>
        <wsa:To>https://vedaxml.com/sys2/idmatrix-v4</wsa:To>
        <wsse:Security soap:mustUnderstand="1">
            <wsu:Timestamp wsu:Id="Timestamp-5fbc1707-8d36-4091-804d-4c19ec06f7bd">
                <wsu:Created>2016-03-23T01:26:31Z</wsu:Created>
                <wsu:Expires>2016-03-23T01:31:31Z</wsu:Expires>
            </wsu:Timestamp>
            <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-4803d49a-e2c3-45f5-bf18-7c0b5cd7d130">
                <wsse:Username>myusername</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">mypassword</wsse:Password>
                <wsse:Nonce>sc5Dgf/vhU+YTZjdLQZsLw==</wsse:Nonce>
                <wsu:Created>2016-03-23T01:26:31Z</wsu:Created>
            </wsse:UsernameToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <request xmlns="http://vedaxml.com/vxml2/idmatrix-v4-0.xsd">
            <individual-name>
                <family-name>Potter</family-name>
                <first-given-name>Harry</first-given-name>
                <other-given-name>James</other-given-name>
            </individual-name>
        </request>
    </soap:Body>
</soap:Envelope>

Its a little closer, but some of the namespaces are still wrong, and the prefexis are wrong too.

It needs to be soapenv: and in the body needs to be idm:

How can I fix the namespaces and prefix's in C#?

So I finally solved my problem. I will post everything I have learnt. First of all, Prefix's do not mean anything. and are the same thing, the prefix only relates to the namespace ie:

xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"   = <s:envelope>
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  = <soap:envelope>

both of these are equivalent.

Secondly what I needed to do was get WS-Security and WS-Addressing and Soap V1.1 (not v1.2) working as this was required by the server. To do this, I had to create a custom binding. In Web.config:

  <system.serviceModel>
<bindings>
  <customBinding>
    <binding name="WsHttpSoap11">
      <textMessageEncoding messageVersion="Soap11WSAddressing10"></textMessageEncoding>
      <security authenticationMode="UserNameOverTransport"
      messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11"
      securityHeaderLayout="Lax"
      includeTimestamp="false"
      requireDerivedKeys="false" >
      </security>
      <httpsTransport></httpsTransport>
    </binding>
  </customBinding>
</bindings>
<client>
  <endpoint address="<my endpoint url>" binding="customBinding" bindingConfiguration="WsHttpSoap11" contract="ServiceReference1.IdMatrixPortType" name="testBinding" ></endpoint>
</client>

I checked the output with Fiddler, and all the correct namespaces were being created.

In conclusion, I hope everyone in the future moves away from SOAP and runs towards Oauth + RESTfull applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM