2011-02-10 43 views
2

フォーム認証で保護され、Internet Explorerの.Net User Controlを介してアクセスするASP.Net互換モードでIISでホストされているWCFサービスを設定しようとしています。 (Secure IIS hosted WCF service for access via IE hosted user control参照)。HTTPリクエストでhttp cookie(ヘッダー)を設定する方法

SilverlightまたはAJAXに匹敵するものが存在しない特定のサードパーティコントロールを使用するため、IEのユーザーコントロールが必要です。

したがって、WCFサービスにアクセスする前に、http要求ヘッダーで認証とセッションID Cookieを設定するには、UserControlが必要です。私のアプローチは、これを行うメッセージインスペクタを設定することです。

だから私は、メッセージインスペクタを定義しました:

public class CookieBehavior : IEndpointBehavior { 
    public void AddBindingParameters(
      ServiceEndpoint endpoint, 
      BindingParameterCollection bindingParameters) { 
    } 

    public void ApplyClientBehavior(
      ServiceEndpoint endpoint, 
      ClientRuntime clientRuntime) { 
     clientRuntime.MessageInspectors.Add(new CookieInspector()); 
    } 

    public void ApplyDispatchBehavior(
      ServiceEndpoint endpoint, 
      EndpointDispatcher endpointDispatcher) { 
    } 

    public void Validate(ServiceEndpoint endpoint) { 
    } 
} 

と私は設定して、コードで私のチャンネルとWCFクライアントを作成します:

public class CookieInspector : IClientMessageInspector { 

    public void AfterReceiveReply(ref Message reply, object correlationState) { 
    } 

    public object BeforeSendRequest(
      ref Message request, 
      IClientChannel channel) { 

     HttpRequestMessageProperty messageProperty; 

     if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) { 
      messageProperty = (HttpRequestMessageProperty) request.Properties[ 
       HttpRequestMessageProperty.Name 
      ]; 
     } 
     else { 
      messageProperty = new HttpRequestMessageProperty(); 
      request.Properties.Add(
       HttpRequestMessageProperty.Name, 
       messageProperty 
      ); 
     } 

     // Set test headers for now... 
     messageProperty.Headers.Add(HttpRequestHeader.Cookie, "Bob=Great"); 
     messageProperty.Headers.Add("x-chris", "Beard"); 

     return null; 
    } 
} 

とエンドポイントの振る舞い

var ea = new EndpointAddress("http://.../MyService.svc"); 

// EDIT: Http cookies can't be set with WSHttpBinding :-(
// var binding = WSHttpBinding(); 
var binding = new BasicHttpBinding(); 


// Disable automatically managed cookies (which enables user cookies) 
binding.AllowCookies = false; 


binding.MaxReceivedMessageSize = 5000000; 
binding.ReaderQuotas.MaxStringContentLength = 5000000; 
var cf = new ChannelFactory<ITranslationServices>(binding, ea); 
cf.Endpoint.Behaviors.Add(new CookieBehavior());  

ITranslationServices service = cf.CreateChannel(); 

しかし、私がFiddlerで私の要求を見ると、httpヘッダーとcookieは設定されていません。私は理由を知りません。私は基本的にそれが動作するはずだと言っているネット、Stackoverflowなどの様々な記事を読んだが、それはしません。私は明白な何かを見逃しているか、またはWCFのバグか何か他のものがありますか?

+0

フォーラムサイトとは異なり、「ありがとうございました」または「何かお手伝いしました」、または[so]の署名は使用しません。 「[ハイ、感謝、タグライン、挨拶を投稿から削除する](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be」を参照してください。 -removed-from-posts)。 –

答えて

3

私はWSHttpBindingの代わりにbasicHttpBindingを使用するとうまくいきました。しかし、理由はわかりません...

+0

WSHttpBindingでこれがうまくいかなかった理由を知りましたか?私は同じ問題を抱えていますが、basicHttpBindingで動作します –

+0

最終的には、 WinForms + WCF + ClickOnceアーキテクチャーと一緒になって、その点は疑問になりました。 –

0

WSHttpBindingは、1つの論理メッセージに対して複数の物理メッセージで構成されています。したがって、連続した物理的な呼び出しが行われた場合、適切にクッキーを持っていない可能性があります。

関連する問題