title: Directory Services Using FindByIdentity method for extended class
body: <p>I've been working with Directory Services since the last few days.</p>
  
  <p>Using the UserPrincipal Object, I've tried to get the email and company field from current AD user. Email is not a problem since it's exposed by default. Company is an other story though.</p>
  
  <p>I found in here a post that explains how to do so: <a href="https://stackoverflow.com/questions/9540436/get-job-title-using-system-directoryservices-accountmanagement">Get job title using System.DirectoryServices.AccountManagement</a></p>
  
  <p>Although, I am having an unfortunate issue with this method. The post shows how to create the FindByIdentity method in the extended class, but in order for that to work you have to set search type to your extended class type, which has for result to find no entries for my specific user. If I set the search type to UserPrincipal in the FindByIdentityWithType, it does find my AD user, but as you can expect, I'm getting an conversion error.</p>
  
  <p>So My question is very simple, Is there any known way to Find by Identity in an extended class?</p>
  
  <p>For Reference here is my extended class:</p>
  
  <pre><code>[DirectoryObjectClass("group")]
  [DirectoryRdnPrefix("CN")]
  public class UserPrincipalClassExtensions : System.DirectoryServices.AccountManagement.UserPrincipal
  {
      PrincipalContext context;
  
      public UserPrincipalClassExtensions(PrincipalContext context)
          : base(context)
      { this.context = context; }
  
      public UserPrincipalClassExtensions(PrincipalContext context, string samAccountName, string Password,bool enabled)
          : base(context, samAccountName, Password, enabled)
      {
  
      }
  
      [DirectoryProperty("company")]
      public string Company
      {
          get
          {
              if (ExtensionGet("company").Length != 1)
                  return null;
  
              return (string)ExtensionGet("company")[0];
  
          }
          set { this.ExtensionSet("company", value); }
      }
  
      // Implement the overloaded search method FindByIdentity.
      public static new UserPrincipalClassExtensions FindByIdentity(PrincipalContext context, string identityValue)
      {
          return (UserPrincipalClassExtensions)FindByIdentityWithType(context, typeof(UserPrincipalClassExtensions), identityValue);
      }
  
      // Implement the overloaded search method FindByIdentity. 
      public static new UserPrincipalClassExtensions FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
      {
          return (UserPrincipalClassExtensions)FindByIdentityWithType(context, typeof(UserPrincipalClassExtensions), identityType, identityValue);
      }
  }
  </code></pre>
  
  <p>This is a Call that Returns null:</p>
  
  <pre><code>    SPUser user = SPContext.Current.Web.CurrentUser;
          using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, getUserDomain(user)))
          {
              UserPrincipalClassExtensions UserInfos = UserPrincipalClassExtensions.FindByIdentity(principalContext,IdentityType.SamAccountName,user.Name);
  
          }
  </code></pre>
  
  <p>This is a Call that Returns a UserPrincipal but without the company field value:</p>
  
  <pre><code>    SPUser user = SPContext.Current.Web.CurrentUser;
          using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, getUserDomain(user)))
          {
              UserPrincipal UserInfos = UserPrincipal.FindByIdentity(principalContext,IdentityType.SamAccountName,user.Name);
  
          }
  </code></pre>
  
  <p>If you need any further info, let me know!</p>
  
  <p>Thanks for in advance!</p>
  
  <p>EDIT</p>
  
  <p>After searching a little more and decompiling de DLL in IL Spy, I've noticed that my code had an issue:</p>
  
  <pre><code>[DirectoryObjectClass("Person")]
  [DirectoryRdnPrefix("CN")]
  </code></pre>
  
  <p>DirectoryObjectClass had to be "Person"</p>
  
  <p>Hope this is going to help some body else!</p>