现在的位置: 首页 > 综合 > 正文

取得AD中某个组织下所有用户的登录名

2012年01月07日 ⁄ 综合 ⁄ 共 1005字 ⁄ 字号 评论关闭

 

    string strLDAP = ConfigurationSettings.AppSettings["LDAP"];
     
string strUserName = ConfigurationSettings.AppSettings["userName"];
     
string strUserPwd = ConfigurationSettings.AppSettings["userPwd"];

     DirectoryEntry entry = new DirectoryEntry(strLDAP, strUserName, strUserPwd);
     DirectorySearcher mySearcher 
= new DirectorySearcher(entry);
     mySearcher.PageSize 
= 99999;  // 默认为1000,此处要注意,可能会造成取用户不全。
     mySearcher.Filter = ("(objectClass=user)"); //user表示用户,group表示组

     SearchResultCollection userCollection 
= mySearcher.FindAll();
     
string[] users = new string[userCollection.Count];

     for (int i = 0; i < userCollection.Count; i++)
     {
           DirectoryEntry oneUser 
= new DirectoryEntry(userCollection[i].Path);
           
if (oneUser.Properties.Contains("userPrincipalName"))
           {
               users[i
= oneUser.Properties["userPrincipalName"].Value.ToString();
           }
           
else
           {
               users[i
= "NULL";
           }
     }

     return users;

抱歉!评论已关闭.