CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Get All Attributes or Properties available in Active Directory in C#
Submitted By Satheesh Babu B
On 11/12/2008 5:41:32 AM
Tags: Active Directory,CodeDigest  

Get All Attributes or Properties available in Active Directory in C#

 

Most of the organisation uses Active Directory to store the user information of their employees as a key value pair in their intranet. This information can be reused in the intranet applications instead maintaining its own user store. .Netframework has verious class and methods to support Active Directory interactions with our applications.

Since Active directory allows any new property to be created for a user in its store, it will be hard to remember the property names to access its value.


The below code snippet will enumerate all the available property names(Keys) and its values.

DirectoryEntry dir = new DirectoryEntry();
        dir.Path = "LDAP://YourActiveDirServername ";       
        DirectorySearcher sea = new DirectorySearcher(dir);
        sea.Filter = "(sAMAccountName=Uname)";
        SearchResult seares = sea.FindOne();     
        StringBuilder str = new StringBuilder();
        ResultPropertyCollection prop = seares.Properties;
        ICollection coll = prop.PropertyNames;
        IEnumerator enu = coll.GetEnumerator();
            while (enu.MoveNext())
            {
                str.Append(enu.Current + " = " + seares.Properties[enu.Current.ToString()][0] + "\n");
            }

         
The above code writes all the available properties and value into the stringbuilder object. Change the LDAP string to your infrastructure name and provide a valid sAMAccountName in the place of Uname for the above code to work.

 

Also include System.DirectoryServices and System.Text namespace with using directive for the above code to work.

 

Happy Coding!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..