【问题标题】:How to log full raw WCF client request from client side?如何从客户端记录完整的原始 WCF 客户端请求?
【发布时间】:2023-03-09 01:48:02
【问题描述】:

我有一个带有 TransportWithMessageCredential 安全模式的 WCF 客户端。当尝试使用 BeforeSendRequest 记录请求时

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    { 
        System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\tmp\\request_log.xml");
        file.WriteLine(request.ToString());
        file.Close();

        return null;
    }

没有安全标签的结果

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">https://(skiped)</Action>
    </s:Header>
    <s:Body>
    ...
    </s:Body>
</s:Envelope>

如何在客户端记录完整的原始请求?应该是这样的

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<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:Id="_0">
<u:Created>...</u:Created>
<u:Expires>..</u:Expires>
</u:Timestamp>
<o:BinarySecurityToken>
<!-- Removed-->
</o:BinarySecurityToken>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
...
</SignedInfo>
<SignatureValue>...</SignatureValue>
<KeyInfo>
<o:SecurityTokenReference>
...
</o:SecurityTokenReference>
</KeyInfo>
</Signature>
</o:Security>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">skiped</Action>
</s:Header>
<s:Body>
...
</s:Body>
</s:Envelope>

UPD。绑定的安全选项

 <security mode="TransportWithMessageCredential">
    <transport clientCredentialType="None" proxyCredentialType="None"
      realm="" />
   <message clientCredentialType="Certificate" algorithmSuite="Basic256" />
 </security>

【问题讨论】:

    标签: c# wcf soap soap-client ws-security


    【解决方案1】:

    我没有找到如何在 C# 中执行此操作,但已使用捕获原始请求 Charles SSL proxying

    请求包含所有安全标签。

    这篇文章对我帮助很大Tracing-WCF-Messages

    【讨论】:

      【解决方案2】:

      This 可能会有所帮助:

      public object BeforeSendRequest(ref Message request, IClientChannel channel)
      {
          MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
          request = buffer.CreateMessage();
          Log("Request:" + Environment.NewLine + buffer.CreateMessage());
          return null;
      }
      

      【讨论】:

      • 同样的结果。
        中只有 标签,没有安全标签
      • 结果显示你的汤头没有安全性!
      • 您可以检查您是否正确添加了安全选项。
      • 选项可能有问题,但我使用我的 app.config 描述从服务器获得了成功的答案。为相关绑定添加了安全选项。不是说传输必须是https,而且消息必须用证书签名吗?
      • HTTPS 是传输层安全协议,而不是应用层安全协议。这意味着消息正文不会在您记录它的位置更改。它将在您的应用程序之外的 Web 服务器处理的 TCP 层中进行加密,您正在将消息记录在您的应用程序中。因此,要查看加密数据包,您需要一个工具来跟踪网络物理层中的数据包,如wireShark。
      【解决方案3】:

      您也可以使用免费的Fiddler。 如果您的端点是 https,请务必使用此绕过证书验证

      ServicePointManager.ServerCertificateValidationCallback = delegate {return true;};
      

      因为 Fiddler 使用自己的证书,该证书对您的连接无效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2015-12-05
        • 2019-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多