2011-03-17 25 views
1

我如何获得使用c#的活动目录用户属性(不是特定用户例如属性)例如cn.cn,邮件等的列表?活动目录属性列表使用c#

+0

我想你应该用“活动目录”而不是asp.net或mvc来标记这个问题,因为这个问题与他们中的任何一个都没有关系。 – neontapir 2011-03-17 05:19:36

+0

或者可能是你应该添加标签ldap – 2011-03-17 05:24:46

回答

1
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

+1

这只会给你这个用户拥有的属性(例如,为这个用户设置的值)。这不会** **枚举在LDAP模式中定义的所有**属性 – 2011-03-17 05:48:33

+0

这只会让您获得该用户的属性......而不是针对所述问题的“所有属性”。 – 2016-12-30 20:24:50

4

如果你在.NET 3.5及以上,则需要在System.DirectoryServices.ActiveDirectory此检查出类。您需要查看像ActiveDirectorySchemaActiveDirectorySchemaClass这样的课程。

您可以通过使用得到当前AD架构的保持:

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema(); 

当你有当前的模式,你可以检查各种类的定义,如:

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

一旦你有该对象,您可以检查,并列举其属性,像:

  • MandatoryProperties
  • 可选属性

等等,以深入了解AD架构。

+1

doh,你比我快3秒:) – 2011-03-17 06:20:05

0

你可以使用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); 
} 
-1
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...

0

拓展业务上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 
...