How to get Active Directory user properties using the System.DirectoryServices.AccountManagement namespace?

I want to get Active Directory properties from a user, and I want to use System.DirectoryServices.AccountManagement .

my code is:

 public static void GetUserProperties(string dc,string user) { PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc); UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user); string firstname = u.GivenName; string lastname = u.Surname; string email = u.EmailAddress; string telephone = u.VoiceTelephoneNumber; ...//how I can get company and other properties? } 
+7
source share
2 answers

You can go to the DirectoryServices namespace to get any property that you need.

 PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc); UserPrincipal u = UserPrincipal.FindByIdentity(ctx, user); string firstname = u.GivenName; string lastname = u.Surname; string email = u.EmailAddress; string telephone = u.VoiceTelephoneNumber; string company = String.Empty; ...//how I can get company and other properties? if (userPrincipal.GetUnderlyingObjectType() == typeof(DirectoryEntry)) { // Transition to directory entry to get other properties using (var entry = (DirectoryEntry)userPrincipal.GetUnderlyingObject()) { if (entry.Properties["company"] != null) company = entry.Properties["company"].Value.ToString(); } } 
+12
source

If you want to change a property, be sure to call userPrincipal.save () after changing the value.

 entry.Properties["company"].value = company; userPrincipal.save(); 
+2
source

All Articles