简体   繁体   中英

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  = a.ghew@mycompany.com";

        // 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.

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

DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(mail=a.ghew@mycompany.com)";
search.SearchScope = SearchScope.Subtree;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM