title: WCF Custom Authorization
body: <p>Basically, I'm creating my first ever WCF web service and I'm looking to implement custom authentication and authorization. The authentication seems to be working well, but I want to be able to store roles and permissions using custom authorization as well.</p>
  
  <p>My authentication is done by overriding <code>UserNamePasswordValidator</code> and making use of the <code>Validate</code> method.</p>
  
  <pre><code>Validate(string UserName, string password)
  </code></pre>
  
  <p>Now I've tried implementing the authorization by using the <code>IAuthorizationPolicy</code> interface</p>
  
  <pre><code>public class AuthorizationPolicy : IAuthorizationPolicy
  {
      private string _id;
  
      public string Id
      {
          get { return this._id; }
      }
  
      public ClaimSet Issuer
      {
          get { return ClaimSet.System; }
      }
  
      public AuthorizationPolicy()
      {
          _id = Guid.NewGuid().ToString();
      }
  
      public bool Evaluate(EvaluationContext context, ref object state)
      {
          IIdentity client = GetClientIdentity(context);
          context.Properties["Principal"] = new CustomPrincipal(client);
  
          return true;
      }
  
      private IIdentity GetClientIdentity(EvaluationContext evaluationContext)
      {
          object obj;
          if (!evaluationContext.Properties.TryGetValue("Identities", out obj))
              throw new Exception("No Identity found");
  
          IList&lt;IIdentity&gt; identities = obj as IList&lt;IIdentity&gt;;
          if (identities == null || identities.Count &lt;= 0)
              throw new Exception("No Identity found");
  
          return identities[0];
      }
  }
  </code></pre>
  
  <p>and I've also implemented the <code>CustomPrincipal</code> using the <code>IPrincipal</code> interface.</p>
  
  <pre><code>public class CustomPrincipal : IPrincipal
  {
      IIdentity _identity;
      string[] _roles;
  
      public CustomPrincipal(IIdentity identity)
      {
          _identity = identity;
      }
  
      public static CustomPrincipal Current
      {
          get
          {
              return Thread.CurrentPrincipal as CustomPrincipal;
          }
      }
  
      public IIdentity Identity
      {
          get { return _identity; }
      }
  
      public string[] Roles
      {
          get
          {
              if (_roles == null)
              {
                  EnsureRoles();
              }
  
              return _roles;
          }
      }
  
      public bool IsInRole(string role)
      {
          EnsureRoles();
  
          return _roles.Contains(role);
      }
  
      protected virtual void EnsureRoles()
      {
          UserManager userManager = new UserManager();
          int userPermissions = userManager.UserPermissions(_identity.Name);
  
          if (userPermissions == 1)
              _roles = new string[1] { "ADMIN" };
          else
              _roles = new string[1] { "USER" };
      }
  }
  </code></pre>
  
  <p>My App.Config has been updated as required, and the <code>Evaluate</code> method in <code>AuthorizationPolicy</code> is called as expected.</p>
  
  <p>However, this is where I'm stuck. How do I go about implementing the roles and permissions from here?</p>