c# – WSHttpBinding TransportWithMessageCredential SecurityMode改变请求
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WSHttpBinding TransportWithMessageCredential SecurityMode改变请求,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3785字,纯文字阅读大概需要6分钟。
内容图文
![c# – WSHttpBinding TransportWithMessageCredential SecurityMode改变请求](/upload/InfoBanner/zyjiaocheng/767/7cef1127090940aab1152c2f5904b187.jpg)
我正在努力与WCF绑定配置.我已添加第三方服务作为我的项目的参考.我得到了一些规格.
我需要使用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呼叫请求,而是安全通道设置的一部分.这是将使用RequestSecurityTokenResponse回答的RequestSecurityToken数据包.之后,如果频道设置正确,则会在您在第一个片段中发布时发送带有方法调用的相同数据包.您可以在Service Trace Viewer中看到此数据包序列:
您尝试解决的实际问题是什么?您的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.
内容总结
以上是互联网集市为您收集整理的c# – WSHttpBinding TransportWithMessageCredential SecurityMode改变请求全部内容,希望文章能够帮你解决c# – WSHttpBinding TransportWithMessageCredential SecurityMode改变请求所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。