自定义绑定 c# WCF

Custom binding c# WCF

我想向我的 WCF SOAP 消息添加安全性 header,如下所示:

<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">URLWithAction</Action>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <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:UsernameToken>
</Security>
</s:Header>

但是,我无法弄清楚这是否可以使用 basicHttpBinding 或其他标准的来完成,或者我是否必须制作自定义的以及如何制作自定义的... 有人能帮忙吗?

如果您想在服务器端或客户端添加消息,请查看此答案

事实证明,我不得不对自定义绑定中的安全元素的属性进行更多尝试。我添加了标签 includeTimestamp="false" 然后安全性 header 起作用了,即使 action 元素没有添加到 header.

自定义绑定的最终结果是:

<customBinding>
 <binding name="BasicHttpBinding_WebService">
  <textMessageEncoding messageVersion="Soap11" writeEncoding="utf-8" />
  <security authenticationMode="UserNameOverTransport" 
            includeTimestamp="false"/>
  <httpsTransport />
 </binding>
 <binding name="BasicHttpBinding_WebService1"/>
</customBinding>