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

标签 c# web-services wse3.0

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

<?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();

当我调用我的网络服务方法时,它们会导致空引用异常。

我希望 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>

提前致谢

最佳答案

好吧,我最终使用了我过去使用过的技术。我创建了实现 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); 
        }
  }

关于c# - 从 WSE 3.0 客户端请求中删除 WS-Addressing/WS-Security 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/753327/

相关文章:

visual-studio - WSE 客户端项目不断将 WebServicesClientProtocol 恢复为 SoapHttpClientProtocol

c# - 如何签署自定义 Soap header ?

c# - 在 C# 中有效使用指向任意内存位置的原始指针

C# BackgroundWorker 和 Com 端口问题

C# 实现依赖于系统的整数

java - 无法从 pgadmin4 连接到 AWS RDS postgres 实例

java - 如何将来自 Web 服务的 'null' 值表示为真正的 null 或空字符串而不是 'null' 字符串

c# - Windows Phone 7 中的 JSON 解析

c# - 如何在 Sharepoint 多行字段中插入新行?

xml-serialization - "The specified type was not recognized"尝试调用 Web 服务调用时出现异常