我需要使用SOAP v1.2所以我认为我需要WSHttpBinding它将是https所以我需要SecurityMode.Transport
像这样的东西:
var binding = new WSHttpBinding(); binding.Security.Mode = SecurityMode.Transport; var client = new MyClient(binding,addr); var result = client.Method(new MyObject());
它产生了这个请求体.
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"> <S:Header> <wsa:MessageID> uuid:6B29FC40-CA47-1067-B31D-00DD010662DA </wsa:MessageID> <wsa:ReplyTo> <wsa:Address>example</wsa:Address> </wsa:ReplyTo> <wsa:To>example</wsa:To> <wsa:Action>example</wsa:Action> </S:Header> <S:Body> <MyObject>...</MyObject> </S:Body> </S:Envelope>
根据我提供的规范,我需要在Header中包含Usernametoken和Timestamp的Security元素.究竟我会用BasicHttpBinding和BasicHttpSecurityMode.TransportWithMessageCredential得到什么
当我尝试使用WSHttpBinding设置TransportWithMessageCredential模式时,请求主体发生了巨大的变化.
binding.Security.Mode = SecurityMode.TransportWithMessageCredential; binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName; var client = new MyClient(binding,addr); client.ClientCredentials.UserName.UserName = "username"; client.ClientCredentials.UserName.Password = "123456"; var result = client.Method(new MyObject());
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"> <S:Header> <wsa:MessageID> uuid:b633067e-9a9e-4216-b036-4afa3aca161e </wsa:MessageID> <wsa:ReplyTo> <wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address> </wsa:ReplyTo> <wsa:To>example</wsa:To> <wsa:Action>http://schemas.xmlsoap.org/ws/2005/02/trust/RST/SCT</wsa:Action> <o:Security s:mustUnderstand=1 xmlns:o=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd> <u:Timestamp>...<u:Timestamp> <o:Usernametoken> <o:Username>...</o:Username> <o:Password>...</o:Password> </o:Usernametoken> </o:Security> </S:Header> <S:Body> <t:RequestSecurityToken>...</t:RequestSecurityToken> </S:Body> </S:Envelope>
现在我的安全部分是正确的,但所有寻址部分都是错误的,身体也是如此.我该怎么办?
我期待(我需要)这样的事情:
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"> <S:Header> <wsa:MessageID> uuid:6B29FC40-CA47-1067-B31D-00DD010662DA </wsa:MessageID> <wsa:ReplyTo> <wsa:Address>example</wsa:Address> </wsa:ReplyTo> <wsa:To>example</wsa:To> <wsa:Action>example</wsa:Action> <o:Security s:mustUnderstand=1 xmlns:o=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd> <u:Timestamp>...<u:Timestamp> <o:Usernametoken> <o:Username>...</o:Username> <o:Password>...</o:Password> </o:Usernametoken> </o:Security> </S:Header> <S:Body> <MyObject>...</MyObject> </S:Body> </S:Envelope>
解决方法
您尝试解决的实际问题是什么?您的WCF呼叫失败了吗?如果是这样,请提供例外细节.最可能的原因是WCF配置或身份验证错误.
如果您努力避免这些协商数据包,请查看此answer,其中显示了如何将SecurityMode切换为Message并将NegotiateServiceCredential选项设置为false.
更新:
这部分与问题更新有关,该问题更新要求修复请求的部分寻址.
要手动设置wsa:ReplyTo标头,您需要设置OperationContext.OutgoingMessageHeaders的ReplyTo属性,如下所示:
using (new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageHeaders.ReplyTo = new EndpointAddress("http://someclient/callback"); var request = client.Method(new MyObject()); }
有关更多详细信息,请参见answer.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。