Active Directory Attribute List Using c#

15,392

Solution 1

If you're on .NET 3.5 and up, you need to check out the classes in System.DirectoryServices.ActiveDirectory for this. You need to look at classes like ActiveDirectorySchema and ActiveDirectorySchemaClass.

You can get hold of the current AD schema by using:

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();

When you have the current schema, you can inspect the various class definitions, e.g.:

ActiveDirectorySchemaClass userSchema = currSchema.FindClass("person");

Once you have that object, you can inspect and enumerate its properties, things like:

  • MandatoryProperties
  • OptionalProperties

and so on to get an insight into the AD schema.

Solution 2

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();
    System.DirectoryServices.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");
        }  

Also, take a look at: http://www.codeproject.com/KB/system/everythingInAD.aspx

Share:
15,392
mahesh
Author by

mahesh

Updated on June 25, 2022

Comments

  • mahesh
    mahesh about 2 years

    How i get the list of active directory user attributes(not of particular user i.e.all attributes) e.g.cn,mail etc. using c#?