2012-04-03 67 views
0

所以,我有這個方法,並在最後一行,它試圖得到samAccountName,它拋出我一個COM異常,這讓我瘋狂。AD查詢,服務器不可操作

有什麼想法?

public User FindUsername(string samAccountName, string groupDisplayName) 
     { 
      using (DirectoryEntry searchRoot = new DirectoryEntry(ldapf, ldapu, ldapp)) 
      { 
       using (DirectorySearcher searcher = new DirectorySearcher(searchRoot)) 
       {    
        searcher.Asynchronous = false; 
        searcher.PropertiesToLoad.Add("SAMAccountName"); 
        searcher.PropertiesToLoad.Add("displayName"); 
        searcher.PropertiesToLoad.Add("uSNChanged"); 
        searcher.PropertiesToLoad.Add("member"); 
        searcher.PropertiesToLoad.Add("co"); 
        searcher.PropertiesToLoad.Add("company"); 
        searcher.PropertiesToLoad.Add("mail"); 


        searcher.Filter = String.Format("(SAMAccountName={0})", samAccountName); 
        searcher.SearchScope = SearchScope.Subtree; 
        searcher.PageSize = 1000; 

        SearchResult result = searcher.FindOne(); 
        ResultPropertyCollection resultPropColl = result.Properties; 
        Object memberColl = resultPropColl["member"]; 
        using (DirectoryEntry memberEntry = new DirectoryEntry("LDAP://" + memberColl, ldapu, ldapp)) 
        { 
         try 
         { 
          System.DirectoryServices.PropertyCollection userprops = memberEntry.Properties; 
          object obVal = userprops["SAMAccountName"].Value; 
+1

什麼是例外? – 2012-04-03 11:37:08

+0

服務器不可操作COMException。 – 2012-04-03 11:40:40

+0

檢查你的屬性,SAMAccountName應該是sAMAccountName(鏈接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms679635(v=vs.85).aspx) – jwillmer 2012-04-03 11:41:41

回答

0

我發現我的代碼是正在別處問題,我錯過了一個foreach

foreach (Object memberColl in resultPropColl["member"]) 
        { 

顯然,如果它返回一個用戶,resultPropColl [「成員」]仍然是一個集合對象?

相關問題