Error 0x80005000 and DirectoryServices

后端 未结 13 1815

I\'m trying to run a simple LDAP query using directory services in .Net.

    DirectoryEntry directoryEntry = new DirectoryEntry(\"LDAP://someserver.contoso.c         


        
相关标签:
13条回答
  • 2020-11-29 05:00

    The same error occurs if in DirectoryEntry.Patch is nothing after the symbols "LDAP//:". It is necessary to check the directoryEntry.Path before directorySearcher.FindOne(). Unless explicitly specified domain, and do not need to "LDAP://".

    private void GetUser(string userName, string domainName)
    {
         DirectoryEntry dirEntry = new DirectoryEntry();
    
         if (domainName.Length > 0)
         {
              dirEntry.Path = "LDAP://" + domainName;
         }
    
         DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);
         dirSearcher.SearchScope = SearchScope.Subtree;
         dirSearcher.Filter = string.Format("(&(objectClass=user)(|(cn={0})(sn={0}*)(givenName={0})(sAMAccountName={0}*)))", userName);
         var searchResults = dirSearcher.FindAll();
         //var searchResults = dirSearcher.FindOne();
    
         if (searchResults.Count == 0)
         {
              MessageBox.Show("User not found");
         }
         else
         {
              foreach (SearchResult sr in searchResults)
              {
                  var de = sr.GetDirectoryEntry();
                  string user = de.Properties["SAMAccountName"][0].ToString();
                  MessageBox.Show(user); 
              }        
         }
    }
    
    0 讨论(0)
  • 2020-11-29 05:01

    I had to change my code from this:

     DirectoryEntry entry = new DirectoryEntry(path, ldapUser, ldapPassword);
     DirectorySearcher searcher = new DirectorySearcher();
     searcher.SearchRoot = entry;
     searcher.SearchScope = SearchScope.Subtree;
    

    To this:

    DirectoryEntry entry = new DirectoryEntry(path, ldapUser, ldapPassword);
    DirectorySearcher searcher = new DirectorySearcher();
    searcher.SearchScope = SearchScope.OneLevel;
    SearchResult searchResult = searcher.FindOne();
    
    0 讨论(0)
  • 2020-11-29 05:04

    This Error can occur if the physical machine has run out of memory. In my case i was hosting a site on IIS trying to access the AD, but the server had run out of memory.

    0 讨论(0)
  • 2020-11-29 05:07

    I had the same again and again and nothing seemed to help.

    Changing the path from ldap:// to LDAP:// did the trick.

    0 讨论(0)
  • 2020-11-29 05:08

    On IIS hosted sites, try recycling the app pool. It fixed my issue. Thanks

    0 讨论(0)
  • 2020-11-29 05:12

    It's a permission problem.

    When you run the console app, that app runs with your credentials, e.g. as "you".

    The WCF service runs where? In IIS? Most likely, it runs under a separate account, which is not permissioned to query Active Directory.

    You can either try to get the WCF impersonation thingie working, so that your own credentials get passed on, or you can specify a username/password on creating your DirectoryEntry:

    DirectoryEntry directoryEntry = 
        new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com", 
                           userName, password);
    

    OK, so it might not be the credentials after all (that's usually the case in over 80% of the cases I see).

    What about changing your code a little bit?

    DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
    directorySearcher.Filter = string.Format("(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", username);
    
    directorySearcher.PropertiesToLoad.Add("msRTCSIP-PrimaryUserAddress");
    
    var result = directorySearcher.FindOne();
    
    if(result != null)
    {
       if(result.Properties["msRTCSIP-PrimaryUserAddress"] != null)
       {
          var resultValue = result.Properties["msRTCSIP-PrimaryUserAddress"][0];
       }
    }
    

    My idea is: why not tell the DirectorySearcher right off the bat what attribute you're interested in? Then you don't need to do another extra step to get the full DirectoryEntry from the search result (should be faster), and since you told the directory searcher to find that property, it's certainly going to be loaded in the search result - so unless it's null (no value set), then you should be able to retrieve it easily.

    Marc

    0 讨论(0)
提交回复
热议问题