user3213767
user3213767

Reputation: 179

Active Directory reading user attributes

I am trying to let my winform system to authonticate using username of the person in active directory. i am using now the following code. But the result is null !!

 private static string LDAP_Connection = "corp.mycompany.global";
 private static string LDAP_Path = "LDAP://OU=USERS,OU=BT,OU=EC,OU=tres,DC=corp,DC=company,DC=global";



 static DirectoryEntry createDirectoryEntry()
        {
            // create and return new LDAP connection with desired settings  

            DirectoryEntry ldapConnection = new DirectoryEntry(LDAP_Connection);
            ldapConnection.Path = LDAP_Path;
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

            return ldapConnection;
        } 



public static void RetreiveUserInfoAdvanced()
{
    try
    {
        // create LDAP connection object  

        DirectoryEntry myLdapConnection = createDirectoryEntry();

        // create search object which operates on LDAP connection object  
        // and set search object to only find the user specified  

        DirectorySearcher search = new DirectorySearcher(myLdapConnection);
        //search.Filter = "(mail  =" + _userlogin + ")";
        search.Filter = "mail  = [email protected]";

        // create results objects from search object  

        //SearchResult result = search.FindOne();

        string[] requiredProperties = new string[] { "cn", "mail" };  

            foreach (String property in requiredProperties)   
               search.PropertiesToLoad.Add(property);  

            SearchResult result = search.FindOne();  

            if (result != null)  
            {  
               foreach (String property in requiredProperties)  
                  foreach (Object myCollection in result.Properties[property])   
                     Console.WriteLine(String.Format("{0,-20} : {1}", property, myCollection.ToString())); 

            }
    }
}

i used Ad Explorer with the same data, everything is find an working fine and i can reach the required data. But from my system can't.

Upvotes: 0

Views: 1751

Answers (1)

Mark S
Mark S

Reputation: 401

I don't have your AD environment, but I did the following in a similar configuration:

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "([email protected])";
search.SearchScope = SearchScope.Subtree;

Give that a go? Basically remove your whitespace in the filter expression and ensure you have traversal enabled.

Upvotes: 1

Related Questions