Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're on .NET 3.5 and up and using the <code>System.DirectoryServices.AccountManagement</code> (S.DS.AM) namespace, you can easily extend the existing <code>UserPrincipal</code> class to get at more advanced properties, like <code>Manager</code> etc.</p> <p>Read all about it here:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/magazine/cc135979.aspx" rel="noreferrer">Managing Directory Security Principals in the .NET Framework 3.5</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx" rel="noreferrer">MSDN docs on System.DirectoryServices.AccountManagement</a></li> </ul> <p>Basically, you just define a derived class based on <code>UserPrincipal</code>, and then you define your additional properties you want:</p> <pre><code>[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); } } </code></pre> <p>Now, you can use the "extended" version of the <code>UserPrincipalEx</code> in your code:</p> <pre><code>using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) { // Search the directory for the new object. UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser"); // you can easily access the Manager or Department now string department = inetPerson.Department; string manager = inetPerson.Manager; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload