Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从客户端记录完整的原始WCF客户端请求?_C#_Wcf_Soap_Soap Client_Ws Security - Fatal编程技术网

C# 如何从客户端记录完整的原始WCF客户端请求?

C# 如何从客户端记录完整的原始WCF客户端请求?,c#,wcf,soap,soap-client,ws-security,C#,Wcf,Soap,Soap Client,Ws Security,我有一个带有TransportWithMessageCredential安全模式的WCF客户端。尝试使用BeforeSendRequest记录请求时 public object BeforeSendRequest(ref Message request, IClientChannel channel) { System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\tmp\\request_log.

我有一个带有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>

https://(跳过)
...
如何在客户端记录完整的原始请求?一定是这样

<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>

可能有助于:

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;
}

我没有发现如何在C#中实现这一点,但使用

请求包含所有安全标签

这篇文章对我帮助很大,你也可以免费使用。 如果您的端点是https,请确保使用此选项绕过证书验证

ServicePointManager.ServerCertificateValidationCallback = delegate {return true;};

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

同样的结果。只有标记,没有安全标记。结果表明,您在汤头中没有安全性!您可以检查是否正确添加了安全选项。选项可能有问题,但我使用app.config说明从服务器获得了成功答案。添加了有关绑定的安全选项。这难道不意味着传输必须是https,并且消息必须用证书签名吗?https是传输层安全协议,而不是应用层安全协议。这意味着在您记录消息的地方不会更改消息正文。它将在TCP层加密,由您的web服务器在您的应用程序之外处理,您正在将消息记录到您的应用程序中。因此,要查看加密的数据包,您需要一个工具来跟踪网络物理层(如wireShark)中的数据包。