使用明文密码对Web服务进行适当的绑定配置

时间:2013-08-26 11:44:55

标签: .net wcf web-services soapui

我正在尝试在.NET中创建一个Web服务客户端。我添加了服务引用,Visual Studio创建了以下绑定:

<bindings>
    <basicHttpBinding>
        <binding name="sample" />
    </basicHttpBinding>
</bindings>

但我得到了q0:FailedAuthentication.

我发现仅当我将WSS_TYPE设置为PasswordText时,我才会在SoapUI中获得结果。

如何在.NET / WCF中设置绑定配置以获取相同的请求?

以下是SoapUI请求:

<soapenv:Envelope xmlns:rnl="sample" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" 
                   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-3">
        <wsse:Username>username</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
        <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">XXXX</wsse:Nonce>
        <wsu:Created>date</wsu:Created>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
  <soapenv:Body>
    <rnl:GetLastUpdate/>
  </soapenv:Body>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:0)

如果没有SSL,请使用ClearUsernameBinding。如果有SSL,请使用:

        <basicHttpBinding>
            <binding name="NewBinding0">
                <security mode="TransportWithMessageCredential" />
            </binding>
        </basicHttpBinding>

并且在任何一种情况下都打开服务器上的wcf日志和跟踪,以查看您的消息与soapUI的不同之处以及内部服务器异常是什么。

答案 1 :(得分:0)

我在以下帮助下找到答案: http://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy/

我需要在BeforeSendRequest

中添加标题
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            UsernameToken token = new UsernameToken(this.Username, this.Password, PasswordOption.SendPlainText);
            XmlElement securityToken = token.GetXml(new XmlDocument());
            MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityToken, false);
            request.Headers.Add(securityHeader);
            return null;
        }
相关问题