从 WSE 3.0 客户端请求中删除 WS-Addressing/WS-Security 部分

发布于 2024-07-16 20:39:10 字数 2284 浏览 8 评论 0原文

我有一个使用 WSDL.exe 创建的简单 C# Web 服务代理类。 我正在远程 Web 服务上调用一个方法,它包含一堆我不想要的 WS-Addressing 和 WS-Security 标头(并且服务器被阻塞)。 以下是原始肥皂请求的示例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action></wsa:Action>
    <wsa:MessageID>urn:uuid:22f12267-b162-4703-a451-2d1c5c5a619b</wsa:MessageID>
    <wsa:To>http://example.com/wstest</wsa:To>
    <wsse:Security>
      <wsu:Timestamp wsu:Id="Timestamp-5c9f0ef0-ab45-421d-a633-4c4fad26d945">
        <wsu:Created>2009-04-15T16:27:25Z</wsu:Created>
        <wsu:Expires>2009-04-15T16:32:25Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

但是我不关心 WS-Addressing/WS-Security 内容。 我没有做任何事情来包含它。 .NET WSE 3.0 包似乎默认添加它们。 有什么办法可以摆脱这些吗? 我在代理对象上看不到允许我删除这些部分的属性。 我已经尝试过:

proxyObject.Addressing.Clear();
proxyObject.Security.Clear();

当我调用 Web 服务方法时,这些会导致空引用异常。

我希望 SOAP 请求如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

提前致谢

I've got a simple C# web service proxy class that I created with WSDL.exe. I am invoking a method on the remote web service, and it is including a bunch of WS-Addressing and WS-Security headers that I do not want (and that the server is choking on). Here is an example of the raw soap request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
  xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <soap:Header>
    <wsa:Action></wsa:Action>
    <wsa:MessageID>urn:uuid:22f12267-b162-4703-a451-2d1c5c5a619b</wsa:MessageID>
    <wsa:To>http://example.com/wstest</wsa:To>
    <wsse:Security>
      <wsu:Timestamp wsu:Id="Timestamp-5c9f0ef0-ab45-421d-a633-4c4fad26d945">
        <wsu:Created>2009-04-15T16:27:25Z</wsu:Created>
        <wsu:Expires>2009-04-15T16:32:25Z</wsu:Expires>
      </wsu:Timestamp>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

But I don't care about the WS-Addressing/WS-Security stuff. I've done nothing to include it. The .NET WSE 3.0 package seems to be adding them by default. Is there any way to get rid of these? I can see no properties on my proxy object that allow me to remove these sections. I've tried:

proxyObject.Addressing.Clear();
proxyObject.Security.Clear();

Those cause a null reference exception when I invoke my web service method.

I want the SOAP request to look like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
  </soap:Header>
  <soap:Body>
    <Func1 xmlns="http://example.com">
      <arg_1 xmlns="">blah</arg_1>
      <arg_2 xmlns="">blah2</arg_2></arg_2>
    </Func1>
  </soap:Body>
</soap:Envelope>

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拧巴小姐 2024-07-23 20:39:10

好吧,我最终使用了我过去使用过的技术。 我创建了实现 SoapFilterPolicyAssertion 的类,它们允许我在发送 SOAP 请求之前修改它的原始 XML。 下面是一个示例:

    public class MyPolicy : SoapFilter
    {
        public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
        {
            // Remove all WS-Addressing and WS-Security header info
            envelope.Header.RemoveAll();

            return SoapFilterResult.Continue;
        }
    }

    public class MyAssertion : PolicyAssertion
    {
        public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
        {
            return new MyPolicy();
        }

        public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
        {
            return null;
        }
    }

然后在您的 Web 服务代理的构造函数中应用该策略:

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]       
 [System.Web.Services.WebServiceBindingAttribute(Name="MyBinding", Namespace="http://example.com")]
    public partial class MyWebClient : WebServicesClientProtocol {

        // ... member variables here

        /// <remarks/>
        public MyWebClient()
        {
            this.Url = "http://example.com";           
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }

            // Apply policy here
            Policy policy = new Policy();
            policy.Assertions.Add(new MyAssertion());
            this.SetPolicy(policy); 
        }
  }

Well, I ended up using a technique I have used in the past. I created classes that implement SoapFilter and PolicyAssertion which allow me to modify the raw XML of the SOAP request before it is sent. Below is an example:

    public class MyPolicy : SoapFilter
    {
        public override SoapFilterResult ProcessMessage(SoapEnvelope envelope)
        {
            // Remove all WS-Addressing and WS-Security header info
            envelope.Header.RemoveAll();

            return SoapFilterResult.Continue;
        }
    }

    public class MyAssertion : PolicyAssertion
    {
        public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
        {
            return new MyPolicy();
        }

        public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
        {
            return null;
        }

        public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
        {
            return null;
        }
    }

Then in your web service proxy's contructor you apply the policy:

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]       
 [System.Web.Services.WebServiceBindingAttribute(Name="MyBinding", Namespace="http://example.com")]
    public partial class MyWebClient : WebServicesClientProtocol {

        // ... member variables here

        /// <remarks/>
        public MyWebClient()
        {
            this.Url = "http://example.com";           
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }

            // Apply policy here
            Policy policy = new Policy();
            policy.Assertions.Add(new MyAssertion());
            this.SetPolicy(policy); 
        }
  }
留一抹残留的笑 2024-07-23 20:39:10

为了删除寻址标头,我在我的应用程序之一中使用了以下代码。

  public override void SecureMessage(SoapEnvelope envelope, Security security)
    {
       //remove addressing Header from envelope

        AddressingHeaders objAH = new AddressingHeaders(envelope);

        objAH.RemoveXml(envelope);


        //Add Wahtever security token you want to add.

        security.Tokens.Add(bla-bla-bla);

    }

我使用 SecureMessage 函数,因为我想添加安全令牌,但可以在 ProcessMessage 函数中使用相同的代码。

to remove the addressing header i used the below code in one of my app.

  public override void SecureMessage(SoapEnvelope envelope, Security security)
    {
       //remove addressing Header from envelope

        AddressingHeaders objAH = new AddressingHeaders(envelope);

        objAH.RemoveXml(envelope);


        //Add Wahtever security token you want to add.

        security.Tokens.Add(bla-bla-bla);

    }

I used SecureMessage function because i wanted to add security tokens,but same code can be used in ProcessMessage function.

时光瘦了 2024-07-23 20:39:10

我想知道您的问题是否也可以通过简单地不使用 WSE 来解决?

I wonder if your problem might not also have ben resolved by simply not using WSE?

她说她爱他 2024-07-23 20:39:10

根据本页中的答案,我添加了以下代码以从 XML 中递归删除特定标头(按标记名)。

Public Overrides Function ProcessMessage(ByVal envelope As SoapEnvelope) As SoapFilterResult
    ' Remove all WS-Addressing and WS-Security header info
    RemoveTag(envelope.DocumentElement, "wsa:Action")
    RemoveTag(envelope.DocumentElement, "wsa:MessageID")
    RemoveTag(envelope.DocumentElement, "wsa:To")
    Return SoapFilterResult.[Continue]
End Function

Private Sub RemoveTag(ByVal XE As System.Xml.XmlElement, ByVal TagName As String)
    For Each N As XmlNode In XE
        If N.ChildNodes.Count > 0 Then
            RemoveTag(N, TagName)
        End If
        If N.Name = TagName Then
            XE.RemoveChild(N)
        End If
    Next
End Sub

Based on the answer in this page, I added the code below to remove specific header (by tagname) recursively from XML.

Public Overrides Function ProcessMessage(ByVal envelope As SoapEnvelope) As SoapFilterResult
    ' Remove all WS-Addressing and WS-Security header info
    RemoveTag(envelope.DocumentElement, "wsa:Action")
    RemoveTag(envelope.DocumentElement, "wsa:MessageID")
    RemoveTag(envelope.DocumentElement, "wsa:To")
    Return SoapFilterResult.[Continue]
End Function

Private Sub RemoveTag(ByVal XE As System.Xml.XmlElement, ByVal TagName As String)
    For Each N As XmlNode In XE
        If N.ChildNodes.Count > 0 Then
            RemoveTag(N, TagName)
        End If
        If N.Name = TagName Then
            XE.RemoveChild(N)
        End If
    Next
End Sub
杀お生予夺 2024-07-23 20:39:10

我发现删除这些寻址和安全标头的最简单方法是更改​​ Web 服务配置以使用 BasicHttpBinding 而不是 WSHttp 绑定

I found the easiest way to remove these addressing and security headers was to change the web service configuration to use BasicHttpBinding instead of WSHttp binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文