加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WSHttpBinding TransportWithMessageCredential SecurityM

发布时间:2020-12-15 21:04:24 所属栏目:百科 来源:网络整理
导读:我正在努力与WCF绑定配置.我已添加第三方服务作为我的项目的参考.我得到了一些规格. 我需要使用SOAP v1.2所以我认为我需要WSHttpBinding它将是https所以我需要SecurityMode.Transport 像这样的东西: var binding = new WSHttpBinding();binding.Security.Mo
我正在努力与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中看到此数据包序列:

enter image description here

您尝试解决的实际问题是什么?您的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读