AD学习--如何获得域中的用户

本文介绍了一段使用 C# 编写的代码,该代码能够连接到活动目录 (Active Directory),并列出所有用户的信息,包括显示名称、电子邮件地址、职位等。

代码出自http://www.c-sharpcorner.com//Code/2003/April/ListingADUsers.asp

Console.Write("Enter your Domain Name : ");
   string dom =Console.ReadLine();

   System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + dom);
   System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
   mySearcher.Filter = ("(objectClass=user)");//filte the objectClass type

//    mySearcher.Filter = ("(&(objectCategory=person)(objectClass=user)(sAMAccountName=xxx))");//filte
the objectClass type
   Console.WriteLine("Listing of users in the Active Directory"); 
   Console.WriteLine("========================================");   
 

   foreach(System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
   {
    try
    {
     System.DirectoryServices.DirectoryEntry de=resEnt.GetDirectoryEntry();
     Console.WriteLine("Display Name  : " + de.Properties["DisplayName"].Value.ToString());
     Console.WriteLine("Email         : " + de.Properties["Mail"].Value.ToString()); 
     Console.WriteLine("Title         : " + de.Properties["Title"].Value.ToString());   
     Console.WriteLine("User Name     : " + de.Properties["sAMAccountName"].Value.ToString());
     Console.WriteLine("First Name    : " + de.Properties["GivenName"].Value.ToString());     
     Console.WriteLine("Last Name     : " + de.Properties["sn"].Value.ToString()); 
     Console.WriteLine("Initials      : " + de.Properties["Initials"].Value.ToString()); 
     Console.WriteLine("Company       : " + de.Properties["Company"].Value.ToString());
     Console.WriteLine("Department    : " + de.Properties["Department"].Value.ToString());
     Console.WriteLine("Telephone No. : " + de.Properties["TelephoneNumber"].Value.ToString()); 
    }
    catch(Exception e)
    {
    }
    Console.WriteLine("===========    End of user   =============");                                  
    
   }
   Console.WriteLine("===========    End of Listing   =============");

 

 

 

From <http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475(v=vs.85).aspx>

Search filterDescription
"(objectClass=*)"All objects.
"(&(objectCategory=person)(objectClass=user)(!cn=andy))"All user objects but "andy".
"(sn=sm*)"All objects with a surname that starts with "sm".
"(&(objectCategory=person)(objectClass=contact)(|(sn=Smith)(sn=Johnson)))"All contacts with a surname equal to "Smith" or "Johnson".
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值