我正在尝试实现 WCF 客户端。服务器需要 WSSE header 。
我可以让它与 HTTP 一起工作。但是它不适用于 HTTPS。
我获得了 2 个用于测试的 URL,一个使用 HTTPS,另一个使用 HTTP。生产服务器将使用 HTTPS。
我正在使用 CustomBinding 使其与 HTTP 配合使用
以下是绑定(bind)代码:
var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
securityElement.EnableUnsecuredResponse = true;
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
var transportElement = new HttpTransportBindingElement();
var binding = new CustomBinding(securityElement, encodingElement, transportElement);
return binding;
当我使用 HTTP URL 时,效果很好。当我尝试使用相同绑定(bind)的 HTTPS 时,出现此错误:
“‘自定义绑定(bind)’。” http://tempuri.org/ '“serviceRequestUpdate_PortType”的绑定(bind)。' http://schemas.officedepot.com/ODTechServices/serviceRequestUpdate '合约配置了需要传输层完整性和 secret 性的身份验证模式。但是传输无法提供完整性和 secret 性。”
我还尝试使用 basichttpbinding for HTTPS,如下所示:
var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
return binding;
现在在这种情况下我收到以下错误:
“安全处理器无法在消息中找到安全 header 。这可能是因为该消息是不安全的错误,或者通信双方之间存在绑定(bind)不匹配。如果服务配置为安全且客户端没有使用安全性。”
我注意到进行了实际的 Web 服务调用并执行了操作。似乎在接收响应方面存在问题。我还注意到,当我发出请求时,我传递了时间戳,但响应没有时间戳,这可能会造成绑定(bind)不匹配的差异。但如果我使用 basicHttpBinding
,则无法设置 IncludeTimestamp = false
。
最佳答案
我得到了答案只需更改 var TransportElement = new HttpTransportBindingElement(); var TransportElement = new HttpsTransportBindingElement();并使用自定义绑定(bind)
关于c# - WCF 自定义绑定(bind)或 basichttpbinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18230462/