How to add active directory (AD) security to WCF services with attribute?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to add active directory (AD) security to WCF services with attribute?

Posted on: June 27, 2013

If you are using WCF web services with active directory (AD) you may want to have a more atomic authorization process than setting the security by IIS. You may want to allow for specific method some group when some other may only have access to others.

The first step is to create a custom attribute. This example show you how to add a single role but you could modify this code to allow multiple roles.

1public class SecureOperationAttribute : Attribute, IOperationBehavior { public string Role { get; set; } public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters){} public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void Validate(OperationDescription operationDescription) {} public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) {
3dispatchOperation.Invoker = new SecureOperationInvoker(Role, dispatchOperation); } }

Once the attribute is set you need to create the invoker.

1public class SecureOperationInvoker : IOperationInvoker {
2 private readonly IOperationInvoker_baseInvoker;
3 private readonly string_operationName;
4 private readonly string_role;
6 public SecureOperationInvoker(string role, DispatchOperation operation) {
7 _role = role;
8 _baseInvoker = operation.Invoker;
9 _operationName = operation.Name;
10 }
12 public object Invoke(object instance, object[] inputs, out object[] outputs) {
13 if (!UserIsInRole(_role)) {
14 throw new FaultException(string.Format("Authentification fail. The operation '{0}' requires the role '{1}'.",_operationName,_role), new FaultCode("Authentification"));
15 }
16 return_baseInvoker.Invoke(instance, inputs, out outputs);
17 }
19 public object[] AllocateInputs() {
20 return_baseInvoker.AllocateInputs();
21 }
23 public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) {
24 return_baseInvoker.InvokeBegin(instance, inputs, callback, state);
25 }
27 public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result) {
28 return_baseInvoker.InvokeEnd(instance, out outputs, result);
29 }
31 public bool IsSynchronous {
32 get { return_baseInvoker.IsSynchronous; }
33 }
35 public bool UserIsInRole(string role) {
36 var winPrinc = new WindowsPrincipal(OperationContext.Current.ServiceSecurityContext.WindowsIdentity);
37 return winPrinc.IsInRole(role);
38 }

Of course, this example a small representation of what should have been done in a real system. In fact, the UserIsInRole shouldn't be hardcoded to use WindowsPrincipal. It should be injected to be able to test without having to use WindowsPrincipal. Also, the role variable may in fact not be exactly the name of the active directory (AD) group, so you may need to inject also a mechanism to translate the role used by the attribute to the AD group.

Here is how to use the attribute over your WCF contract.

2public interface IMyEntityXYZ {
3 [OperationContract]
4 [SecureOperation(Role = "Admin")]
5 void Action(DTOXYZ[] xyz);

What I like is that you can set the security on the contract. It's simple for the maintenance cause the operation signature contains who has access the the method defined. It's also easy to test if you are using injection to create a stub method for the validation of the role.