WCF - Passing CurrentPrincipal in the Header

Posted by David Ward on Stack Overflow See other posts from Stack Overflow or by David Ward
Published on 2010-04-22T09:17:21Z Indexed on 2010/04/29 21:07 UTC
Read the original article Hit count: 559

Filed under:
|
|

I have a WCF service that needs to know the Principal of the calling user.

In the constructor of the service I have:

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

and in the calling code I have something like:

        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();
            }
        }

This all works fine.

My question is, how can I avoid having to wrap all proxy method calls in the using (var scope...{ [create header and add to OperationContext]?

Could I create a custom ChannelFactory that will handle adding the myPrincipal header to the operation context? Something like that would save a whole load of copy/paste which I'd rather not do but I'm not sure how to achieve it:)

Thanks

© Stack Overflow or respective owner

Related posts about wcf

Related posts about wcf-security