2009-04-15 66 views
4

我有一個簡單的C#web服務代理類,使用WSDL.exe創建。我正在調用遠程Web服務上的一個方法,它包含了一堆我不想要的WS-Addressing和WS-Security標頭(並且服務器正在窒息)。這裏是原始SOAP請求的例子:從WSE 3.0中刪除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服務方法時,這些會導致null引用異常。

我想要的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> 

在此先感謝

+0

這可能有所幫助:http://blogs.msdn.com/b/dhrubach/archive/2008/06/16/modifying-the-security-header-generated-by-wse-runtime.aspx – 2012-10-01 14:01:54

+0

如果你是使用WCF然後這個SO問題可能會幫助你http://stackoverflow.com/questions/24635950/remove-timestamp-element-from-ws-security-headers-created-by-wcf – Ruskin 2014-07-11 08:15:33

回答

3

嗯,我結束了使用我已經在過去使用的技術。我創建了實現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); 
     } 
    } 
0

我不知道你的問題可能不是也有奔通過簡單地不使用WSE解決?

0

根據本頁的答案,我添加了下面的代碼,以便從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 
0

我發現刪除這些尋址和安全標題最簡單的方法是改變Web服務配置使用BasicHttpBinding而不是WSHttp binding

1

刪除尋址標題我在我的應用程序中使用了下面的代碼。

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的功能使用。