2012-04-17 6 views
5

私はいくつかのSTSサービスに対してクライアントを構築していますが、現在は1日以上にわたってWCFメッセージにヘッダーを追加しようとしています。 RequestSecurityTokenへの私の呼び出しで、私はUsernameTokenを含める必要があります。WCFヘッダーの詳細を操作する

私はそれを達成する方法がわかりません。その瞬間、私はエンドポイントの動作とメッセージインスペクタを定義しました。後者のBeforeSendRequest()では、MessageHeaderから派生したカスタムクラス 'Security'のオブジェクトを作成します。セキュリティにはUsernameTokenのインスタンスが含まれます。

public class MessageInspector : IClientMessageInspector { 

public object BeforeSendRequest(ref Message request, IClientChannel channel) { 
    Security uns = new Security(); 
    uns.UsernameToken = new UsernameToken(); 

    // ... 

    var Header = new MessageHeader<Security>(uns); 
    var untyped = Header.GetUntypedHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); 
    request.Headers.Add(untyped); 
    return null; 
} 
} 

public class Security : MessageHeader { 
public UsernameToken UsernameToken = new UsernameToken(); 

public override string Name { 
    get { return "Security"; } 
} 

public override string Namespace { 
    get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; } 
} 
} 

public class UsernameToken { 
public String Username = ""; 
public Password Password = new Password(); 
} 

これは特にのUsernameTokenの名前空間が間違っているように思わ

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">urn:RequestSecurityToken</Action> 
    <Security xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <UsernameToken xmlns="http://schemas.datacontract.org/2004/07/Tarifrechner.Kfz"> 
     <Password> 
      <Type>http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText</Type> 
      <password>******</password> 
     </Password> 
     <Username>******</Username> 
     </UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body /> 
</s:Envelope> 

シリアル化されているものです。私はそれがデータコントラクトのシリアル化から来るが、私は別の名前空間が必要であることを知っている。

これは私がシリアライズされたデータは

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="..."> 
    <soap:Header> 
    <Security xmlns:q1="http://www.bipro.net/namespace" xsi:type="q1:UserNameSecurity" 
      xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <UsernameToken> 
     <Username>******</Username> 
     <Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">******</Password> 
     </UsernameToken> 
    </Security> 
    <wsa:Action>urn:RequestSecurityToken</wsa:Action> 
    <wsse:Security> 
     <wsu:Timestamp wsu:Id="Timestamp-b9dd599d-5901-451d-8321-6a309091f273"> 
     <wsu:Created>2012-03-11T16:02:56Z</wsu:Created> 
     <wsu:Expires>2012-03-11T16:07:56Z</wsu:Expires> 
     </wsu:Timestamp> 
    </wsse:Security> 
    </soap:Header> 
    <soap:Body> 
    <RequestSecurityToken xmlns="http://schemas.xmlsoap.org/ws/2005/02/trust"> 
     <TokenType>http://schemas.xmlsoap.org/ws/2005/02/sc/sct</TokenType> 
     <RequestType> 
     http://schemas.xmlsoap.org/ws/2005/02/trust/Issue 
     </RequestType> 
    </RequestSecurityToken> 
    </soap:Body> 
</soap:Envelope> 

ようになりたいものですが、右についての私のアプローチですか?ヘッダーの詳細の名前空間やデータが属性や要素としてシリアル化されているかどうかなどを操作するにはどうすればよいですか?ラディスラフは、すでに述べたように

更新


、私はUsernameTokenを私のようなクラスを実装する必要はありません。 WCFに関する私の知識が限られているため、私はそれをしました。

これまでのところ、SecurityMode.TransportWithMessageCredentialとEstablishSecurityContextをfalseに設定して使用するように設定されたWS2007HttpBindingが、ほとんど私が探しているXMLを生成することを発見しました。私はそれをどのように知っていますか?

しかし、残っている問題が1つあります。リクエストに空のbody要素があります。リクエストにはbody要素内にRequestSecurityToken要素があります。誰も知っている、どのように私はそれを達成することができますか?

EstablishSecurityContext = trueを使用すると同時に、Soap-Actionを目的の "urn:RequestSecurityToken"から非動作 "http://docs.oasis-open.org/ws-sx/ws"に変更します-trust/200512/RST/SCT」を参照してください。


回答ありがとうございます。

ありがとうございます!

ビョルン

+1

のhttp:/ /stackoverflow.com/questions/5228430/how-to-add-attribute-to-wcf-message-header-with-messageheader-createheader-met これが役に立ちます – v00d00

+3

なぜこれを手動でやっていますか? WCFセキュリティ設定を設定すると、それらのヘッダーが追加されます。 –

答えて

0

あなたはヘッダーの内容を制御する必要があり、ヘッダを挿入する必要がどこかに応じて、メッセージにヘッダを追加するには、いくつかの異なる方法があります。

アプリケーションコードでは、要求のプロパティの一部を変更するために、要求の周囲にOperationContextScopeを作成できます。 OperationContextScope内には、OutgoingMessageHeadersコレクションを介してメッセージヘッダーを操作できるOperationContext.Currentの有効なインスタンスがあります。このメソッドを使用すると、ヘッダーをアプリケーションコードに深く制御できます。必要な場所に適切なコードをコピーする責任を負う必要があります。(WCFチームの誰かから)

Thesetwoリンクコードサンプルのトンしてより詳細に本について話:

1

1つの代替方法は、あなたのことができますあなたの要求のためのMessageContractタイプを定義することですSOAPメッセージのヘッダーと本文に表示される内容を定義し、使用される名前空間を調整します。たとえば、次のサービスの定義を考えてみます。

[ServiceContract] 
    public interface IMyService 
    { 
     [OperationContract] 
     MyResponse DoSomething(MyRequest request); 
    } 

    public class MyService : IMyService 
    { 
     public MyResponse DoSomething(MyRequest request) 
     { 
      return new MyResponse() 
      { 
       Details = "Service did something awesome.", 
       Timestamp = DateTime.Now 
      }; 
     } 
    } 

    [MessageContract(IsWrapped = true, WrapperNamespace = "http://myservice/messages/")] 
    public class MyRequest 
    { 
     [MessageHeader(Namespace = "http://myservice/security")] 
     public string TokenThingy 
     { 
      get; 
      set; 
     } 
    } 

    [MessageContract(IsWrapped = true, WrapperNamespace = "http://myservice/messages")] 
    public class MyResponse 
    { 
     [MessageBodyMember] 
     public string Details 
     { 
      get; 
      set; 
     } 

     [MessageBodyMember] 
     public DateTime Timestamp 
     { 
      get; 
      set; 
     } 
    } 

要求を送信すると、次のSOAP生成:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IMyService/DoSomething</Action> 
    <h:TokenThingy xmlns:h="http://myservice/security">fda</h:TokenThingy> 
    </s:Header> 
    <s:Body> 
    <MyRequest xmlns="http://myservice/messages/" /> 
    </s:Body> 
</s:Envelope> 

をし、サービスからの応答は次のようになります。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header /> 
    <s:Body> 
    <MyResponse xmlns="http://myservice/messages"> 
     <Details xmlns="http://tempuri.org/">Service did something awesome.</Details> 
     <Timestamp xmlns="http://tempuri.org/">2012-05-04T17:04:36.5980424-04:00</Timestamp> 
    </MyResponse> 
    </s:Body> 
</s:Envelope> 
関連する問題