Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active directory findone() method

im trying to inquire Ad by using this line`s

            DirectoryEntry de = null;
            SearchResult results = null;
            de = new DirectoryEntry();

            //geting the result FROM ad
            de.Path = dr.manager;
            de.AuthenticationType = AuthenticationTypes.Secure;
            DirectorySearcher search = new DirectorySearcher(de);
            search.Filter = string.Format("(objectClass={0})",'*');
            search.PropertiesToLoad.Add("IsraelID");
            results = search.FindOne();
            de = results.GetDirectoryEntry();

but im getting an exception in the findone()

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
   at System.DirectoryServices.DirectorySearcher.FindOne()
like image 677
MIkCode Avatar asked Oct 23 '25 01:10

MIkCode


2 Answers

"Unspecified error" means that your LDAP path is missing the LDAP protocol identifier. Ensure that your path contains the LDAP protocol identifier in upper case.

Example:

DirectoryEntry de = null; 
SearchResult results = null; 
de = new DirectoryEntry(); 

// Assuming your domain dns name is treyresearch.net 
de.Path = "LDAP://servername/CN=users,DC=treyresearch,DC=net"; 
de.AuthenticationType = AuthenticationTypes.Secure; 
de.Username = "treyresearch\\Administrator";
de.Password = "P@$$W0rd";
DirectorySearcher search = new DirectorySearcher(de); 
search.Filter = string.Format("(objectClass={0})",'*'); 
search.PropertiesToLoad.Add("IsraelID"); 
results = search.FindOne(); 
de = results.GetDirectoryEntry(); 
like image 154
Hans Avatar answered Oct 26 '25 02:10

Hans


string LDAP = "LDAP://DC=MYDOMAIN,DC=COM";
using (DirectoryEntry dirEntry = new DirectoryEntry(LDAP, null, null, AuthenticationTypes.Secure))
    using (DirectorySearcher dirSearch = new DirectorySearcher(
        dirEntry,
        string.Concat("(objectClass=*)"),
        new string[] { "IsraelID" }))
    {
        SearchResult result = dirSearch.FindOne();
        if (result != null)
            return result.Properties["IsraelID"][0].ToString();
        else
            return null;
    }

Note: The string.Concat() around the "(objectClass=*)" statement is there because It's common to add additional statements or variables there.

Make sure you have a proper LDAP string, and I would suggest using statements to make sure you dispose of everything afterwards.

like image 41
Sivvy Avatar answered Oct 26 '25 02:10

Sivvy