简体   繁体   English

使用c#的Active Directory属性列表

[英]Active Directory Attribute List Using c#

我如何使用C#获取活动目录用户属性(不是特定用户即所有属性)的列表,例如cn,mail等?

If you're on .NET 3.5 and up, you need to check out the classes in System.DirectoryServices.ActiveDirectory for this. 如果您使用的是.NET 3.5及更高版本,则需要在System.DirectoryServices.ActiveDirectory检出此类。 You need to look at classes like ActiveDirectorySchema and ActiveDirectorySchemaClass . 您需要查看类似ActiveDirectorySchemaActiveDirectorySchemaClass类。

You can get hold of the current AD schema by using: 您可以使用以下方法来掌握当前的AD模式:

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();

When you have the current schema, you can inspect the various class definitions, eg: 当拥有当前模式时,可以检查各种类定义,例如:

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

Once you have that object, you can inspect and enumerate its properties, things like: 拥有该对象后,您可以检查并枚举其属性,例如:

  • MandatoryProperties MandatoryProperties
  • OptionalProperties OptionalProperties

and so on to get an insight into the AD schema. 等等,以深入了解AD模式。

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 此外,请查看: http : //www.codeproject.com/KB/system/everythingInAD.aspx

Expanding on marc_s 's answer here. 在这里扩展marc_s的答案。 Here is a complete code example that prints the common name and the actual attribute name. 这是一个完整的代码示例,其中显示了通用名称和实际属性名称。

ActiveDirectorySchema schema = ActiveDirectorySchema.GetCurrentSchema();
ActiveDirectorySchemaClass person = schema.FindClass("user");
foreach( ActiveDirectorySchemaProperty property in person.GetAllProperties() )
{
    Console.WriteLine("{0} = {1}", property.CommonName, property.Name);
}

Example output. 示例输出。

Common-Name = cn
Instance-Type = instanceType
NT-Security-Descriptor = nTSecurityDescriptor
Object-Category = objectCategory
Object-Class = objectClass
Object-Sid = objectSid
SAM-Account-Name = sAMAccountName
Account-Expires = accountExpires
...

You could use WMI: 您可以使用WMI:

 ObjectGetOptions objectGetOptions = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
 ManagementClass managementClass = new ManagementClass("root\\directory\\LDAP", "ads_user", objectGetOptions);

 foreach (PropertyData dataObject in managementClass.Properties)
 {
    Console.WriteLine(dataObject.Name);
 }

While ADExplorer does not list all the available attributes, I have found it a great tool for seeing what goes where. 尽管ADExplorer并未列出所有可用的属性,但我发现它是查看行进路线的好工具。

You can download it from http://technet.microsoft.com/en-us/sysinternals/bb963907.aspx 您可以从http://technet.microsoft.com/en-us/sysinternals/bb963907.aspx下载它

UserPropertyList = new List<string>();

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();

ICollection Collection = currSchema.FindAllProperties();

IEnumerator Enumerator = Collection.GetEnumerator();

while (Enumerator.MoveNext())
{
   UserPropertyList.Add(Enumerator.Current.ToString());
}

The above code will add all search attributes of Active Directory to the UserPropertyList... 上面的代码会将Active Directory的所有搜索属性添加到UserPropertyList...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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