2012-06-17 28 views
0

我是一名新成員,雖然我遵循這個很多:) 我的代碼連接到活動目錄以獲取功能組的成員,只給我1490個單數列表中實際有1680名奇怪成員中的成員。我在Stackoverflow和Internet上搜索了很多,但我還沒有找到答案,爲什麼代碼會導致不完整的列表。可以,任何人都可以給我任何指示。謝謝:)從活動目錄中檢索數據給出了不完整的列表

這裏是連接到Active Directory檢索數據的代碼:

public static DataTable GetAdUsers(string configSection) 
    { 

     DataRow dr; 
     Hashtable ADGroups = (Hashtable)ConfigurationManager.GetSection(configSection); 
     string adGroup; 
     string adGroupDesc; 
     string sApplication; 
     string sLast_Login; 
     string sAccount_owner; 
     string sPath; 

     DataTable dt = new DataTable(); 

     sApplication = "Application"; 
     dt.Columns.Add(sApplication); 

     dt.Columns.Add("Profile", Type.GetType("System.String")); 
     dt.Columns.Add("Account Name", Type.GetType("System.String")); 

     sLast_Login = "Last Login"; 
     dt.Columns.Add(sLast_Login); 

     sAccount_owner = "Account Owner"; 
     dt.Columns.Add(sAccount_owner); 

     sPath = "Path"; 
     dt.Columns.Add(sPath); 

     string domainName = "myDomain"; 

     PrincipalContext pcRoot = new PrincipalContext(ContextType.Domain, domainName); 
     IDictionaryEnumerator adGroupEnumerator = ADGroups.GetEnumerator(); 

     while (adGroupEnumerator.MoveNext()) 
     { 
      adGroup = adGroupEnumerator.Key.ToString(); 
      adGroupDesc = adGroupEnumerator.Value.ToString(); 

      GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, IdentityType.SamAccountName, adGroup); 
      System.DirectoryServices.DirectoryEntry de = (System.DirectoryServices.DirectoryEntry)grp.GetUnderlyingObject(); 
      foreach (string sDN in de.Properties["member"]) 
      { 
       System.DirectoryServices.DirectoryEntry deMember = new System.DirectoryServices.DirectoryEntry("LDAP://" + sDN.ToString()); 
       try 
       { 
        dr = dt.NewRow(); 

        string output1; 
        string subStringE1 = "DC="; 
        int length1 = de.Path.ToString().Length; 
        int length0 = de.Path.ToString().IndexOf(subStringE1); 
        string str1 = de.Path.ToString().Substring(length0, length1 - length0); 
        string subStringE2 = ",DC"; 
        int length2 = str1.ToString().IndexOf(subStringE2); 
        output1 = str1.ToString().Substring(3, length2 - 3); 

        dr["Application"] = "Application"; 
        dr["Profile"] = adGroupDesc; 

        string AccountName = deMember.Properties["samAccountName"].Value.ToString(); 

        dr["Account Name"] = deMember.Properties["samAccountName"].Value.ToString(); 
        dr["Last Login"] = ""; 
        dr["Account Owner"] = deMember.Properties["givenName"].Value.ToString() + @"-" + deMember.Properties["sn"].Value.ToString(); 

        string Path = output1 + @"\" + adGroup + @"\" + deMember.Properties["samAccountName"].Value.ToString(); 

        Console.WriteLine(Path); 
        dr["Path"] = output1 + @"\" + adGroup + @"\" + deMember.Properties["samAccountName"].Value.ToString(); 

        dt.Rows.Add(dr); 
       } 
       catch (Exception ex) 
       { 
        Console.WriteLine("Error occured for user name" + sDN + "\n" + ex.Message); 
       } 
      } 
     } 
     return dt; 
    } 
} 
+0

所以你使用.NET 3.5'PrincipalContext'和所有 - 然後** **爲什麼你切換回舊式.NET 2.0 'DirectoryEntry'一旦你有成員的名字?根本沒有任何意義,使事情變得更加繁瑣!你已經有一個'GroupPrincipal grp' - 你爲什麼不使用'.GetMembers()'調用來獲得組的成員?會**更容易!** –

+0

嗨馬克,你有任何指針/鏈接在同一個。我收到的大部分海蔘結果都在.NET 2.0上。另外Afifi在Msdn中的鏈接也在.NET 2.0上。 – Ashutosh

+0

請參閱此MSDN雜誌文章:[在.NET Framework 3.5中管理目錄安全主體](http://msdn.microsoft.com/zh-cn/magazine/cc135979.aspx) –

回答

0

由於的sizeLimit的默認限制的,你可能只是圍繞1,490物體或因此讓。要解決這個問題,你需要在結果中「翻頁」。

只需使用在Enumerating Members in a Large Group處引用的代碼即可。

+0

非常感謝Afifi :)只是在相同的工作。該鏈接非常有用 – Ashutosh

+0

鏈接非常有用,但groupMember.FindOne()會導致未知錯誤。 – Ashutosh

+0

但是,當我使用鏈接中提供的代碼時,出現錯誤「用戶名成員發生錯誤;範圍= 0- *」 – Ashutosh

0

如果可能返回的結果集包含1000個以上的項目,則必須使用分頁搜索。在沒有分頁的情況下執行的活動目錄搜索限於返回最多1000條記錄。通過分頁搜索,結果集被呈現爲單獨的頁面,每個頁面包含預定數量的結果條目。使用這種類型的搜索,返回結果條目的新頁面,直到達到結果集的末尾。

默認情況下,響應查詢請求的服務器在返回數據之前完全計算結果集。在較大的結果集中,這需要獲取結果集時的服務器內存以及返回較大結果時的網絡帶寬。設置頁面大小允許服務器在頁面生成時以頁面形式發送數據。客戶端然後緩存這些數據併爲應用程序級代碼提供一個遊標。

通過定義在數據通過網絡返回給客戶端之前服務器計算多少行來設置分頁。

+0

謝謝Jakub,致力於您和Afifi的想法和鏈接:) – Ashutosh

0

找到答案通過研究和嘗試我的運氣。 使用目錄條目是個問題,但並不是要使用頁面搜索,儘管我嘗試了相同的方法。

工作代碼如下:

GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, adGroup); //fglbcmdolpctx 
       if (grp != null) 
       { 
        foreach (Principal p in grp.GetMembers(true)) 
        { 
         try 
         { 
          dr = dt.NewRow(); 
          dr["Application"] = "Commodities OpenLink"; 
          dr["Profile"] = adGroupDesc; 
          string AccountName = p.SamAccountName.ToString().ToLower(); 
          dr["Account Name"] = AccountName; 
          dr["Last Login"] = ""; 
          string sLastName, sFirstName; 
          int iLastNameIndex0, iLastNameIndex1, iFirstNameIndex0, iFirstNameIndex1; 
          int lengthofString = p.Name.ToString().Length; 
          iLastNameIndex1 = p.Name.ToString().IndexOf(","); 


          if (iLastNameIndex1 == -1) 
          { 
           sLastName = ""; 
          } 
          else 
          { 
           sLastName = p.Name.ToString().Substring(0, iLastNameIndex1); 
          } 

          iFirstNameIndex0 = p.Name.ToString().IndexOf(","); 
          iFirstNameIndex1 = p.Name.ToString().IndexOf(":"); 
          if (iFirstNameIndex0 == -1 || iFirstNameIndex1 == -1) 
          { 
           sFirstName = p.Name.ToString(); 
           sLastName = ""; 
          } 
          else 
          { 
           sFirstName = p.Name.ToString().Substring(iFirstNameIndex0 + 1, iFirstNameIndex1 - iFirstNameIndex0 - 1); 
          } 

          sAccount_owner = sLastName + @"-" + sFirstName; 
          dr["Account Owner"] = sAccount_owner; 

          string sPath_Domain_Part; 
          string sFirstIndexofExtraction = "DC="; 
          int ilength_String = p.DistinguishedName.ToString().Length; 
          int iLenght_ExtractionPoint1 = p.DistinguishedName.ToString().IndexOf(sFirstIndexofExtraction); 
          string str1 = p.DistinguishedName.ToString().Substring(iLenght_ExtractionPoint1, ilength_String - iLenght_ExtractionPoint1); 

          string subStringE2 = ",DC"; 
          int iLenght_ExtractionPoint2 = str1.IndexOf(subStringE2); 
          sPath_Domain_Part = str1.Substring(3, iLenght_ExtractionPoint2 - 3); 
          string sPath1 = sPath_Domain_Part + @"\" + adGroup + @"\" + p.SamAccountName.ToString(); 
          dr["Path"] = sPath_Domain_Part + @"\" + adGroup + @"\" + p.SamAccountName.ToString(); 
          dt.Rows.Add(dr); 
         } 

         catch (Exception ex) 
         { 
          Global.logfile.WriteLine("Error occured for user name" + adGroup + p.SamAccountName + "\n" + ex.Message); 
         } 
        } 
       } 
+0

不要忘記將您的答案標記爲已接受 – j0k

相關問題