简体   繁体   中英

How to get Active Directory User Account Attributes in C#

I can successfully get a complete list of all AD Attributes with the following code (including things like extensionAttribute1 - 15 )

// get whatever attributes are available

List<string> allAttributes = new List<string>();

var context = new DirectoryContext(DirectoryContextType.Forest, "mydomain.com");

using (var schema = System.DirectoryServices.ActiveDirectory.ActiveDirectorySchema.GetSchema(context)) {

    var userClass = schema.FindClass("user");

    foreach (ActiveDirectorySchemaProperty property in userClass.GetAllProperties()) {
        allAttributes.Add(property.Name);
    }

}

However, when I retrieve a user account with the following code, most of these attributes (especially the extensionAttribute s ) are not present:

SearchResultCollection results;
DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE");
DirectorySearcher ds = new DirectorySearcher("LDAP://" + de.Properties["defaultNamingContext"][0].ToString());

ds.Filter = "(&(objectCategory=User)(objectClass=person))";

results = ds.FindAll();

foreach (SearchResult sr in results) {
    Console.WriteLine(sr.Properties["extensionAttribute1"][0].ToString()); // == null
}

What am I doing wrong?

most of these attributes (especially the extensionAttributes) are not present

That is normal. Attributes are only returned if they have a value. If an attribute does not have a value, it is not returned at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM