laneherby
laneherby

Reputation: 485

How can I find a single user in Active Directory by common name using PrincipalSearcher?

I'm trying to use System.DirectoryServices to find the manager of a user. So I use the current code to get the manager.

 string test = string.Empty;
 using(var context = new PrincipalContext(ContextType.Domain))
 {
   using(var user = UserPrincipal.FindByIdentity(context, "user"))
   {                    
     DirectoryEntry de = (DirectoryEntry)user.GetUnderlyingObject();
     test = de.Properties["manager"][0].ToString();
   }
 }

The test variable then contains only the CN or common name of the manager. I need to use that common name to get the actual user of the manager. Because I need things like UserID and Email etc.. How can I accomplish that?

Upvotes: 0

Views: 553

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40988

The manager attribute contains the distinguished name of the manager's account (the common name is just the CN= portion of the distinguished name).

You can use UserPrincipal.FindByIdentity() with that distinguished name, just like you did with the user:

var manager = UserPrincipal.FindByIdentity(context, test);

Upvotes: 2

Related Questions