2011-09-01 166 views
1

我想重新寫從一個的System.DirectoryServices搜索System.DirectoryServices.ProtocolSystem.DirectoryServices.Protocol搜索問題

在S.DS我得到的所有請求的屬性後面,但在S.DS .P,我沒有得到GUID或HomePhone ...

它的其餘部分適用於一個用戶。

什麼想法?

public static List<AllAdStudentsCV> GetUsersDistinguishedName(string domain, string distinguishedName) 
     { 
      try 
      { 

       NetworkCredential credentials    = new NetworkCredential(ConfigurationManager.AppSettings[ "AD_User" ], ConfigurationManager.AppSettings[ "AD_Pass" ]); 
       LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier(domain+":389"); 

       using (LdapConnection connection   = new LdapConnection(directoryIdentifier, credentials)) 
       { 

        SearchRequest searchRequest = new SearchRequest(); 
        searchRequest.DistinguishedName = distinguishedName; 
        searchRequest.Filter = "(&(objectCategory=person)(objectClass=user)(sn=Afcan))";//"(&(objectClass=user))"; 
        searchRequest.Scope = SearchScope.Subtree; 
        searchRequest.Attributes.Add("name"); 
        searchRequest.Attributes.Add("sAMAccountName"); 
        searchRequest.Attributes.Add("uid"); 
        searchRequest.Attributes.Add("telexNumber"); // studId 
        searchRequest.Attributes.Add("HomePhone"); //ctrId 
        searchRequest.SizeLimit = Int32.MaxValue; 
        searchRequest.TimeLimit = new TimeSpan(0, 0, 45, 0);// 45 min - EWB 

        SearchResponse searchResponse = connection.SendRequest(searchRequest) as SearchResponse; 

        if (searchResponse == null) return null; 

        List<AllAdStudentsCV> users = new List<AllAdStudentsCV>(); 

        foreach (SearchResultEntry entry in searchResponse.Entries) 
        { 
         AllAdStudentsCV user = new AllAdStudentsCV(); 

         user.Active = "Y"; 
         user.CenterName = ""; 
         user.StudId = GetstringAttributeValue(entry.Attributes, "telexNumber"); 
         user.CtrId = GetstringAttributeValue(entry.Attributes, "HomePhone"); 
         user.Guid = GetstringAttributeValue(entry.Attributes, "uid"); 
         user.Username = GetstringAttributeValue(entry.Attributes, "sAMAccountName"); 

         users.Add(user); 
        } 

        return users; 
       } 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 
     } 

另外,如果我想獲取在廣告的每個用戶,這樣我就可以用我的SQL數據庫同步數據,我怎麼做,我一直得到最大規模突破,錯誤。我將大小設置爲maxInt32 ...是否有「忽略大小」選項?

感謝,

Eric-

+0

改變「HOMEPHONE」到「HOMEPHONE」現在我回家的電話,但還是老樣子沒有GUID ...任何想法都能在這裏找到,而不是「UID」叫什麼名字? –

+1

它應該是objectGUID –

+1

要容易地找到屬性名稱(即使那些AD不會使用常規工具(如ldapbrowser)顯示),您應該嘗試使用ADSIEdit(DC上的adsiedit.msc)瀏覽目錄 –

回答

1

我認爲,標準的方法是使用的System.DirectoryServices,不System.DirectoryServices.Protocol。爲什麼你想用後者?

關於第二個關於錯誤消息「超出最大值」的問題,可能是因爲您試圖一次獲取太多條目。
Active Directory限制查詢返回的對象數,以便不超載目錄(限制類似於1000個對象)。獲取所有用戶的標準方式是使用分頁搜索。

的算法是這樣的:

  1. 您構建將獲取所有用戶查詢
  2. 指定要在此查詢特定控制(分頁結果控制),表明這是 一個分頁搜索,每頁500個用戶
  3. 您啓動查詢,獲取第一頁並解析 中的前500個條目,該頁面爲
  4. 您要求AD申請下一頁,解析下一個500 en試圖
  5. 重複,直到沒有剩下的頁面
+1

S.AD.P應該是更快,我的第一次傳球被S.DS打擊......但需要15分鐘才能獲得。我們需要抓取所有的學生,所以我猜想分頁是最好的選擇。找到一些代碼示例: –

+0

http://dunnry.com/blog/CommentView,guid,1707c3e7-5395-45f4-8882-3a17f291934b.aspx –

+0

http://dunnry.com/blog/CategoryView,category,Protocols.aspx –