WCF C#Soap服务无法正确呈现

时间:2016-03-23 01:28:49

标签: c# wcf soap

我正在尝试对Web服务进行SOAP调用。

根据手册,SOAP调用应如下所示:

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

该公司为我提供了一个WSDL文件。我已将其导入C#作为服务引用和Web引用(因为我无法使服务引用工作)

首先,我尝试了一个服务引用,当我导入WSDL文件时,没有在web.config中创建绑定,所以我以编程方式添加了绑定。这是我的代码:

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

resposne失败了,所以我用Fiddler检查了POST。它与所需的帖子完全不同。

菲德勒告诉我:

POST https://ctaau.vedaxml.com/cta/sys2/idmatrix-v4 HTTP / 1.1 Content-Type:text / xml;字符集= utf-8的 SOAPAction:&#34; http://vedaxml.com/idmatrix/VerifyIdentity&#34; 主持人:ctaau.vedaxml.com 内容长度:1216 期待:100-继续 Accept-Encoding:gzip,deflate 连接: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>

正如您所看到的,WSSE名称空间不正确。我没有WS-Addressing而且前缀都错了?

然后我转到了Web引用。我需要让WS-Secrutiy工作,所以我将客户端协议更改为WebServicesClientProtocol。

这是我的代码:

  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看起来像:

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>

它更接近一些,但是一些命名空间仍然是错误的,而且优势也是错误的。

它需要是soapenv:并且身体需要是idm:

如何修复C#中的命名空间和前缀?

1 个答案:

答案 0 :(得分:0)

所以我终于解决了我的问题。我会发布我所学到的一切。首先,前缀并不意味着什么。  并且是相同的,前缀只与名称空间相关,即:

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

这两者都是等价的。

其次我需要做的是获取WS-Security和WS-Addressing以及Soap V1.1(不是v1.2),因为这是服务器所要求的。 为此,我必须创建一个自定义绑定。在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>

我用Fiddler检查了输出,并且正在创建所有正确的命名空间。

总之,我希望未来的每个人都能远离SOAP并向Oauth + RESTfull应用程序运行。