Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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# 如何在http下添加自定义soap头或使用用户名身份验证?_C#_Wcf_Visual Studio 2008_Wcf Security_Ws Security - Fatal编程技术网

C# 如何在http下添加自定义soap头或使用用户名身份验证?

C# 如何在http下添加自定义soap头或使用用户名身份验证?,c#,wcf,visual-studio-2008,wcf-security,ws-security,C#,Wcf,Visual Studio 2008,Wcf Security,Ws Security,我正在尝试在BeforeSendRequest方法中修改soap消息并向其添加自定义头,如下所示: public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel) { MessageHeader header = MessageHeader.CreateHeader("name", "http://myname", "

我正在尝试在
BeforeSendRequest
方法中修改soap消息并向其添加自定义头,如下所示:

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
    {
        MessageHeader header = MessageHeader.CreateHeader("name", "http://myname", "Oscar");
        request.Headers.Add(header);

        return null;
    }
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="51efbfdb-2187-45b9-81fc-6a38815d5bed" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">d1351a78-a53a-4d32-ad35-fca6d4262adf</ActivityId>
<name xmlns="http://myname">Oscar</name>
</s:Header>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="51efbfdb-2187-45b9-81fc-6a38815d5bed" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">d1351a78-a53a-4d32-ad35-fca6d4262adf</ActivityId>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:Username xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">username</wsse:Username>
      <wsse:Password xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">password</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>
</s:Header>
此时一切正常,soap标头中添加的标头如下所示:

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
    {
        MessageHeader header = MessageHeader.CreateHeader("name", "http://myname", "Oscar");
        request.Headers.Add(header);

        return null;
    }
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="51efbfdb-2187-45b9-81fc-6a38815d5bed" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">d1351a78-a53a-4d32-ad35-fca6d4262adf</ActivityId>
<name xmlns="http://myname">Oscar</name>
</s:Header>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="51efbfdb-2187-45b9-81fc-6a38815d5bed" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">d1351a78-a53a-4d32-ad35-fca6d4262adf</ActivityId>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:Username xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">username</wsse:Username>
      <wsse:Password xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">password</wsse:Password>
    </wsse:UsernameToken>
</wsse:Security>
</s:Header>


  • CreateHeader的第三个参数是一个对象。默认情况下,此对象是使用DataContractSerializer序列化的,因此,如果您创建一个对标头结构建模的数据协定,则可以将实例作为第三个参数传递,它将创建您想要的标头。CreateHeader的第三个参数是一个对象。默认情况下,此对象是使用DataContractSerializer序列化的,因此如果您创建一个对标头结构建模的数据协定,您可以将一个实例作为第三个参数传递,它将创建您想要的标头

    我也遇到了同样的问题,尽管我的服务是https。这应该是一个配置问题,您不需要手动处理

    无论如何,您不必使用消息检查器使用BeforeSendRequest。 您应该尝试通过添加一些安全配置来更改配置生成的代码。 然后,WCF将创建您需要的标题。然后在代码中添加凭据,您应该可以了

    我想象生成的代理正在为您创建customBinding或basicHttpBinding。 basicHttpBinding虽然不支持WS-Security规范,但应该将其更改为custom或wsHttpBinding。您可以使用并找到正确的配置以获得所需的结果

    我添加的是“示例配置语法”:

    
    ........
    
    这是一个示例配置,例如,您可能不需要包含时间戳,或者您可能需要一个Nonce,如果我没有记错的话,它需要更多的effore,不过不需要太多

    要查看此讨论,请执行以下操作:


    希望这有帮助。

    我也有同样的问题,尽管我的服务是https。这应该是一个配置问题,您不需要手动处理

    无论如何,您不必使用消息检查器使用BeforeSendRequest。 您应该尝试通过添加一些安全配置来更改配置生成的代码。 然后,WCF将创建您需要的标题。然后在代码中添加凭据,您应该可以了

    我想象生成的代理正在为您创建customBinding或basicHttpBinding。 basicHttpBinding虽然不支持WS-Security规范,但应该将其更改为custom或wsHttpBinding。您可以使用并找到正确的配置以获得所需的结果

    我添加的是“示例配置语法”:

    
    ........
    
    这是一个示例配置,例如,您可能不需要包含时间戳,或者您可能需要一个Nonce,如果我没有记错的话,它需要更多的effore,不过不需要太多

    要查看此讨论,请执行以下操作:


    希望这有帮助。

    是解决方案吗

    是解决方案吗?你是说像这样的?这对我来说是新的:-)谢谢分享。我有一个问题要问你,你是否使用fiddler跟踪soap消息并显示为我想要的结果?(和它应该很相似,你应该试试。这对我来说是新的:-)谢谢你分享这个。我有一个问题要问你,你是否使用fiddler跟踪soap消息并显示为我想要的结果?(与之非常相似,您应该尝试一下。我需要使用“UserNameOverTransport”的https吗?我想是的,但此WS在http下,不兼容。我需要使用“UserNameOverTransport”的https吗?我想是的,但此WS在http下,不兼容。