WSE 3.0 クライアント要求から WS-Addressing/WS-Security セクションを削除する

StackOverflow https://stackoverflow.com/questions/753327

  •  09-09-2019
  •  | 
  •  

質問

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 サービス メソッドを呼び出すときに 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>

前もって感謝します

役に立ちましたか?

解決

さて、私は過去に使用したテクニックを使用することになりました。を実装するクラスを作成しました ソープフィルター そして ポリシーアサーション これにより、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); 
        }
  }

他のヒント

アドレス指定ヘッダーを削除するには、アプリの 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);

    }

私が使用した セキュアメッセージ セキュリティトークンを追加したかったので関数を使用しましたが、同じコードを次の場所で使用できます。 プロセスメッセージ 関数。

WSE を使用しないだけでは問題は解決されなかったのでしょうか?

このページの回答に基づいて、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

これらのアドレス指定とセキュリティ ヘッダーを削除する最も簡単な方法は、使用するように Web サービスの構成を変更することであることがわかりました。 BasicHttpBinding の代わりに WSHttp binding.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top