If you use .NET 3.5 and later and use the System.DirectoryServices.AccountManagement (S.DS.AM) namespace, you can easily extend the existing UserPrincipal class to get more advanced properties, such as Manager , etc.
Read more here:
Basically, you just define a derived class based on UserPrincipal , and then define your additional properties that you want:
[DirectoryRdnPrefix("CN")] [DirectoryObjectClass("Person")] public class UserPrincipalEx : UserPrincipal { // Inplement the constructor using the base class constructor. public UserPrincipalEx(PrincipalContext context) : base(context) { } // Implement the constructor with initialization parameters. public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) {} // Create the "Department" property. [DirectoryProperty("department")] public string Department { get { if (ExtensionGet("department").Length != 1) return string.Empty; return (string)ExtensionGet("department")[0]; } set { ExtensionSet("department", value); } } // Create the "Manager" property. [DirectoryProperty("manager")] public string Manager { get { if (ExtensionGet("manager").Length != 1) return string.Empty; return (string)ExtensionGet("manager")[0]; } set { ExtensionSet("manager", value); } } // Implement the overloaded search method FindByIdentity. public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue) { return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue); } // Implement the overloaded search method FindByIdentity. public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue) { return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue); } }
Now you can use the "advanced" version of UserPrincipalEx in your code:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) {
source share