2010-04-22 18 views
3

我有一個WCF服務,需要知道主叫用戶的主體。WCF - 在頭部傳遞CurrentPrincipal

在服務的構造函數中我有:

Principal = OperationContext.Current.IncomingMessageHeaders.GetHeader<MyPrincipal>("myPrincipal", "ns"); 

,並在調用代碼我有類似:

 using (var factory = new ChannelFactory<IMyService>(localBinding, endpoint)) 
     { 
      var proxy = factory.CreateChannel(); 
      using (var scope = new OperationContextScope((IContextChannel)proxy)) 
      { 
       var customHeader = MessageHeader.CreateHeader("myPrincipal", "ns", Thread.CurrentPrincipal); 
       OperationContext.Current.OutgoingMessageHeaders.Add(customHeader); 

       newList = proxy.CreateList(); 
      } 
     } 

這一切工作正常。

我的問題是,我該如何避免必須在using (var scope...{ [create header and add to OperationContext]中包裝所有代理方法調用?

我可以創建一個自定義ChannelFactory來處理將myPrincipal頭添加到操作上下文嗎?類似的東西會節省複製/粘貼的整個負載,我寧願不這樣做,但我不知道該如何實現呢:)

感謝

回答

3

正確的時間設置一個WCF主要是通過IAuthorizationPolicy,通過在配置中指定自定義策略。這完整涵蓋了here。如果您嘗試在其他位置設置委託人(也許是檢查員),則可以由系統重置。