How to get list of classes of an Active Directory

2020-04-21 07:18发布

问题:

I was trying get the list of classes and attributes of an Active Directory.

DirectoryEntry entry = new DirectoryEntry(
        "LDAP://CN=Schema,CN=Configuration,DC=addomain,DC=com",
        null, null, AuthenticationTypes.Secure);

ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();
ActiveDirectorySchemaClass User = schema.FindClass("account");

foreach (ActiveDirectorySchemaProperty property in User.GetAllProperties())
{
    Console.WriteLine("{0}", property.Name);
}

This returns all the attributes of a specified class. How do I get all the classes that exist in Active Directory?

回答1:

How do I get all the classes that exist in Active Directory?

You need to modify the same code which you've used. You need to find all classes for the schema, as I've shown below. It'd return a read-only collection that contains ActiveDirectorySchemaClass objects, whose individual items you need to read.

DirectoryEntry entry = new DirectoryEntry(
        "LDAP://CN=Schema,CN=Configuration,DC=addomain,DC=com",
        null, null, AuthenticationTypes.Secure);

ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();

// below code retrieves all Active Directory Domain Services classes in the schema.
ReadOnlyActiveDirectorySchemaClassCollection collection = schema.FindAllClasses();

// Now you can iterate over the collection Items.
foreach (ActiveDirectorySchemaClass schemaClass in collection)
   {
       foreach (ActiveDirectorySchemaProperty property in schemaClass.GetAllProperties())
          {
              Console.WriteLine("{0}", property.Name);
          }
   }

Please refer to ReadOnlyActiveDirectorySchemaClassCollection Members from MSDN for more detail.