8

我有一个 WCF SOAP 使用者,它是由 Visual Studio 2012 从 WSDL 实现的。WSDL 由 PeopleTools 生成。基础对象的类型为System.ServiceModel.ClientBase

我需要 SOAP 请求类似于:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://xmlns.oracle.com/Enterprise/Tools/schemas">
    <soapenv:Header>
        <wsse:Security soap:mustUnderstand="1" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>[plain text username goes here]</wsse:Username>
                <wsse:Password>[plain text password goes here]</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <sch:InputParameters>
            <Last_Name>Aren</Last_Name>
            <First_Name>Cambre</First_Name>
        </sch:InputParameters>
    </soapenv:Body>
</soapenv:Envelope>

这是我们能得到的最接近的:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
        <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
        <a:MessageID>urn:uuid:3cc3f2ca-c647-466c-b38b-f2423462c837</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To s:mustUnderstand="1">http://[internal URL to soap listener]</a:To>
    </s:Header>
    <s:Body>
        <t:RequestSecurityToken Context="uuid-7db82975-2b22-4236-94a1-b3344a0bf04d-1" xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
            <t:TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</t:TokenType>
            <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
            <t:KeySize>256</t:KeySize>
            <t:BinaryExchange ValueType=" http://schemas.xmlsoap.org/ws/2005/02/trust/tlsnego" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">FgMBAFoBAABWAwFQ9IhUFGUO6tCH+0baQ0n/3us//MMXzQA78Udm4xFj5gAAGAAvADUABQAKwBPAFMAJwAoAMgA4ABMABAEAABX/AQABAAAKAAYABAAXABgACwACAQA=</t:BinaryExchange>
        </t:RequestSecurityToken>
    </s:Body>
</s:Envelope>

你会注意到两个问题:

  • 没有明文 WSSE 凭据。传递服务不会使用的凭据的二进制形式。
  • 身份验证在Body,不是Header
  • 请求省略InputParameters

这是基本的 C# 代码:

var service = new ServiceWithBizarreNameFromPeoplesoft();

if (service.ClientCredentials == null)
   throw new NullReferenceException();
service.ClientCredentials.UserName.UserName = "test";
service.ClientCredentials.UserName.Password = "password";

var binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential) {Security = new WSHttpSecurity()};
service.Endpoint.Binding = binding;

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Mode = SecurityMode.Message;

var input = new InputParameters { Last_Name = "Cambre", First_Name = "Aren" };

var returnData = service.BizarrePeopleSoftNameForMethod(input);

没有 HTTP 层安全性,传输是 SSL 加密的。身份验证仅基于 SOAP 消息。

4

2 回答 2

11

那是对 WS-SecureConversation 令牌的请求。默认情况下使用它WSHttpSecurity,除非您将其EstablishSecurityContext属性更改为false. 请改用此绑定:

var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);    
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

它将使用带有 UserName 令牌的 SOAP 1.1,并且需要 HTTPS 传输。

编辑:

对于没有 HTTPS 的测试,请尝试使用此自定义绑定:

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.AllowInsecureTransport = true;

var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
var transportElement = new HttpTransportBindingElement();

var binding = new CustomBinding(securityElement, encodingElement, transportElement);
于 2013-01-15T09:37:56.377 回答
-1

在我看来,这就像使用基本用户名密码身份验证的具有传输安全性的 wsHttpBindings。

这些行在我看来是错误的:

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.Security.Mode = SecurityMode.Message;

这是我希望在您的应用程序或 web.config 中看到此配置的方式

<bindings>
  <wsHttpBinding>
    <binding name="ws" >
      <security mode="Transport">
        <transport clientCredentialType="Basic" proxyCredentialType="Basic" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint address="http://www.bla.com/webservice" binding="basicHttpBinding" contract="bla.IService" name="ws" />
</client>

然后代码将如下所示:

var service = new GeneratedProxyClient("basic");
service.ClientCredentials.UserName.UserName = "test";
service.ClientCredentials.UserName.Password = "password";
var input = new InputParameters { Last_Name = "Cambre", First_Name = "Aren" };
var returnData = service.BizarrePeopleSoftNameForMethod(input);

在这里可能会更好地解释-> http://msdn.microsoft.com/en-us/library/ms733775.aspx

于 2013-01-15T04:02:59.410 回答