簡體   English   中英

使用c#的Active Directory屬性列表

[英]Active Directory Attribute List Using c#

我如何使用C#獲取活動目錄用戶屬性(不是特定用戶即所有屬性)的列表,例如cn,mail等?

如果您使用的是.NET 3.5及更高版本,則需要在System.DirectoryServices.ActiveDirectory檢出此類。 您需要查看類似ActiveDirectorySchemaActiveDirectorySchemaClass類。

您可以使用以下方法來掌握當前的AD模式:

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();

當擁有當前模式時,可以檢查各種類定義,例如:

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

擁有該對象后,您可以檢查並枚舉其屬性,例如:

  • MandatoryProperties
  • OptionalProperties

等等,以深入了解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");
        }  

此外,請查看: http : //www.codeproject.com/KB/system/everythingInAD.aspx

在這里擴展marc_s的答案。 這是一個完整的代碼示例,其中顯示了通用名稱和實際屬性名稱。

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

示例輸出。

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
...

您可以使用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);
 }

盡管ADExplorer並未列出所有可用的屬性,但我發現它是查看行進路線的好工具。

您可以從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());
}

上面的代碼會將Active Directory的所有搜索屬性添加到UserPropertyList...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM