2012-09-05 120 views

回答

5

請在LDAP提供用戶名密碼可以與服務器進行通信,並具有管理員權限的連接字符串。

假設DC是me.com用戶名密碼是由具有管理員權限該用戶ID的密碼。

DirectoryEntry rootDSE = rootDSE = new DirectoryEntry("LDAP://OU="",OU=" ",dc="me",dc=com", username, password); 

    DirectorySearcher search = new DirectorySearcher(rootDSE); 

    search.PageSize = 1001;// To Pull up more than 100 records. 

    search.Filter = "(&(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))";//UserAccountControl will only Include Non-Disabled Users. 
     SearchResultCollection result = search.FindAll(); 

     foreach (SearchResult item in result) 
     { 
      if (item.Properties["cn"].Count > 0) 
      { 
       DisplayName = item.Properties["cn"][0].ToString(); 
      } 
      if (item.Properties["mail"].Count > 0) 
      { 
       EmailAddress = item.Properties["mail"][0].ToString(); 
      } 
      if (item.Properties["SamAccountName"].Count > 0) 
      { 
       DomainName = item.Properties["SamAccountName"][0].ToString(); 
      } 
      if (item.Properties["department"].Count > 0) 
      { 
       Department = item.Properties["department"][0].ToString(); 
      } 
      if (item.Properties["title"].Count > 0) 
      { 
       title = item.Properties["title"][0].ToString(); 
      } 
      if (item.Properties["company"].Count > 0) 
      { 
       company = item.Properties["company"][0].ToString(); 
      } 
      if (item.Properties["DistinguishedName"].Count > 0) 
      { 
       memberof = item.Properties["DistinguishedName"][0].ToString(); 
      } 
      if (item.Properties["AccountExpirationDate"].Count > 0) 
      { 
       string aaa = item.Properties["AccountExpirationDate"][0].ToString(); 
      } 

       dt.Rows.Add(DisplayName, EmailAddress, DomainName, Department, title, company, memberof); 
      DisplayName = string.Empty; 
      EmailAddress = string.Empty; 
      DomainName = string.Empty; 
      Department = string.Empty; 
      title = string.Empty; 
      company = string.Empty; 
      memberof = string.Empty; 

       rootDSE.Dispose(); 

通過這種方式,我們可以從我們的域服務器上拉出所有記錄。

相關問題