Zulfiqar's weblog

Architecture, security & random .Net

PrincipalPermission Authorization in WF 4

Posted by zamd on January 12, 2010

In last post, I have shown you how to get hold of OperationContext when using messaging activities. Once you got hold of OperationContext, you can use it to perform many useful tasks and one of them is Authorization. Let’s start by defining a Scope activity to hook our IReceiveMessageCallback implementation. 



    public class PrincipalPermissionScope : NativeActivity


        public InArgument<string> PrincipalPermissionName { get; set; }

        public InArgument<string> PrincipalPermissionRole { get; set; }


        public Activity Body { get; set; }


        protected override void Execute(NativeActivityContext context)


            var name = this.PrincipalPermissionName.Get(context);

            var role = this.PrincipalPermissionRole.Get(context);


            var principalPermission = new PrincipalPermission(name, role);


                new AuthorizationManager(principalPermission));





As part of Scope activity execution, I have added my ReceiveMessageCallback in execution properties collection. This enables our callback to be called for every message received by any activity inside the Scope. Once we have access to OperationContext (which is the only as the only parameter of the callback) we can use it to perform authorization.



        class AuthorizationManager : IReceiveMessageCallback



            PrincipalPermission principalPermission;


            public AuthorizationManager(PrincipalPermission principalPermission)


                this.principalPermission = principalPermission;



            public void OnReceiveMessage(

                System.ServiceModel.OperationContext operationContext,

                ExecutionProperties activityExecutionProperties)


                var currentPrincipal = Thread.CurrentPrincipal;

                var isPrincipalSet = false;

                var targetPrincipal = GetPrincipal(operationContext);



                    if (targetPrincipal != null)


                        Thread.CurrentPrincipal = targetPrincipal;

                        isPrincipalSet = true;






                catch (SecurityException)


                    throw SecurityUtility.CreateAccessDeniedFaultException();




                    if (isPrincipalSet)

                        Thread.CurrentPrincipal = currentPrincipal;





And with a customer designer, this is how it look like

2 Responses to “PrincipalPermission Authorization in WF 4”

  1. zamd said


    I have some doubt with respect to authenticating a WCF Service through service bus from ADFS.If you are interested to help me, please ping me in the given email id.. Will put my detailed explanation on that.

  2. zamd said

    great post,
    i would using this tecnique in my xamlx for save the "workflow initiator" in a variable always available. Is possible to see the code of designer PrincipalPermissionScopeDesigner? i’m a newbie and i suppose that writing that from scratch are very difficult.

    Please help me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: