我正在使用WS-Security编写一个使用非.Net Web服务的WCF
客户端。服务的响应包含一个Security标头,其mustUnderstand设置为true。
使用ServiceModelListener,我确实看到了从服务返回的实际数据。但是,WCF客户端失败,因为它没有处理Security标头。
<env:Header>
<wsse:Security env: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">
<wsu:Timestamp wsu:Id="timestamp">
<wsu:Created>2012-03-28T13:43:54.474Z</wsu:Created>
<wsu:Expires>2012-03-28T13:48:54.474Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</env:Header>
WCF客户端错误消息:
此消息的接收者无法理解名称空间'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'中的标头'Security',导致该消息未得到处理。此错误通常表示此消息的发送者已启用接收者无法处理的通信协议。请确保客户端绑定的配置与服务的绑定一致。
我的WCF
客户端不需要任何时间戳信息。有没有简单的方法可以在处理例程中存根?我已经尝试扩展Response类并添加[MessageHeader]属性。
编辑:
提出另一种方式:如何实现一个接受标记为必须理解的自定义标头元素的
WCF
客户端?
WS-Security有不同的标准。在客户端更改绑定可能是有意义的,因为basicHttpBinding和wsHttpBindings可以使用不同的安全标准。
我遇到了类似的问题。我不确定这是否有用。
MSDN WCF可扩展性
http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx
这里的设置是基于证书的,Oracle应用服务器10g和.Net以使用服务。在尝试弄清请求和响应的情况时,使用SOAPUi非常有用。
我没有尝试修改代码以使用basicHttpBinding,但是我将WSHttpBinding用作代码中配置的基础。然后用
WSHttpBinding binding = new WSHttpBinding()
{
CloseTimeout = new TimeSpan(0, 1, 0),
OpenTimeout = new TimeSpan(0, 1, 0),
SendTimeout = new TimeSpan(0, 1, 0),
AllowCookies = false,
BypassProxyOnLocal = false,
HostNameComparisonMode = HostNameComparisonMode.StrongWildcard,
MaxBufferPoolSize = 524288,
MaxReceivedMessageSize = 65536,
MessageEncoding = WSMessageEncoding.Text,
UseDefaultWebProxy = false,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxDepth = 32,
MaxArrayLength = 16384,
MaxBytesPerRead = 4096,
MaxNameTableCharCount = 16384,
MaxStringContentLength = 8192
}
};
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = string.Empty;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
binding.Security.Message.EstablishSecurityContext = true;
binding.Security.Message.NegotiateServiceCredential = true;
CustomBinding customBinding = new CustomBinding();
BindingElementCollection collection = binding.CreateBindingElements();
为TextMessageEncodingBindingElement遍历以将Soap11和AddressingVersion设置为None。
foreach (BindingElement element in collection)
{
if (typeof(TextMessageEncodingBindingElement) == element.GetType())
{
TextMessageEncodingBindingElement item = element as TextMessageEncodingBindingElement;
if (null != item)
{
item.MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
customBinding.Elements.Add(item);
}
}
else
customBinding.Elements.Add(element);
}
我使用ChannelFactory并为Message Inspector添加了Endpoint Behavior。此时,我可以控制请求了,可以添加适当的头并修改Action上的mustUnderstand。
[使用SOAPUi,我将Message.ToString()放入SOAPUI并测试了请求。一旦将所需的项目添加到请求中,然后确定OAS服务器未使用所有必需的元素进行答复。使用消息检查器进行答复,我修改了消息以包括缺少的标题。我不记得在哪里找到了消息检查器的基本代码,但是您需要修改代码以正确使用它。
在我的示例中,一些片段。
对于]中的转换消息>
public object BeforeSendRequest
我需要修改Header,因此使用for循环,我抓住了XElement并添加了OASIS头并添加了To头。
XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/"; XElement securityHeader = new XElement( xmlns + "Security", new XAttribute(xmlns + "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"), new XAttribute(xmlns + "xmlns", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"), new XAttribute(xmlns + "mustUnderstand", "0")); element.Add(securityHeader);
我还必须修改动作标题
else if (localName.Equals("Action", StringComparison.InvariantCultureIgnoreCase)) { foreach (XAttribute a in element.Attributes()) { if (a.Name.LocalName == "mustUnderstand") a.Value = "0"; } }
我的问题是服务未使用操作标头答复
所以在
public void AfterReceiveReply
我将TransformReply返回类型Message称为类似以下内容的东西。您可能需要修改string.Empty的值,但这只是一个示例。
...
Message reply = Message.CreateMessage(message.Version, null, reader); reply.Headers.Add(MessageHeader.CreateHeader("Action", string.Empty, string.Empty, false)); reply.Properties.CopyProperties(message.Properties);
...
我真的建议您使用诸如SOUPUI之类的工具来弄乱信封并查看答复。如果您使用SSL,则需要创建一个cacert文件并将其放置在首选项的SSLSettings中。
遇到有关支持ONVIF的IP摄像机的一些代码的问题。摄像头发回了Nonce并在“安全性”元素中创建,WCF不喜欢它。最后使用IClientMessageInspector捕获响应,并将标头重新标记为mustUnderstand = false。