Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the Active Directory schema programmatically

I did some programming for reading the data from Active Directory such as user account or Orgnization info and so on. The code below is like something what I did.

DirectoryEntry entry = new DirectoryEntry(
    "LDAP://CN=Users,DC=domain,DC=com",
    null,
    null,
    AuthenticationTypes.Secure
    );

DirectorySearcher search = new DirectorySearcher(entry);

using (SearchResultCollection src = search.FindAll())
{
    foreach (SearchResult result in src)
    {
        Console.WriteLine(result.Properties["name"][0] + " : " + 
                          result.Properties["department"][0]);
    }
}

The problem is how can I know what properties that target objects have then I can use them to filter the data before get it all.

Any ideas?

like image 439
Edison Chuang Avatar asked Jul 20 '10 14:07

Edison Chuang


People also ask

How do I access my Active Directory schema?

Type MMC at the command prompt and then press Enter to start a blank MMC console. In the MMC, on the File menu select Add/Remove Snap-in. From the available snap-ins, select the Active Directory Schema snap-in, click Add, and then click OK. Now you can manage your Active Directory schema.

What are the schemas in Active Directory?

A schema is the definition of attributes and classes that are part of a distributed directory and is similar to fields and tables in a database. Schemas include a set of rules which determine the type and format of data that can be added or included in the database.


1 Answers

If you have a DirectoryEntry, you can inspect its .SchemaEntry:

DirectoryEntry entry = new DirectoryEntry("LDAP://......");

DirectoryEntry schema = entry.SchemaEntry;

This should - if you have the necessary permissions - give you access to the properties defined in the schema - things like MandatoryProperties or OptionalProperties:

foreach (var prop in schema.Properties.PropertyNames)
{
   string propName = prop.ToString();
   var propValue = schema.Properties[propName].Value;
}

Does that help you get started??

You might also want to have a look at BeaverTail - my C# open-source LDAP browser.

alt text
(source: mvps.org)

It will allow you to inspect any LDAP node and see all its properties.

like image 133
marc_s Avatar answered Oct 13 '22 00:10

marc_s