2012-04-03 139 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 [“成员”]仍然是一个集合对象?

相关问题