1

In Active directory, I am trying to connect to LDAP server and trying to check if user us valid or not. But the below code throws the exception error. I am not sure where i am going wrong.. Even whether the connection made is correct or not. I tried the same connection name and password into the Apache directory studio which i am able to connect successfully but not through the code.It throws the exception from myLdapConnection please can you check where i am going wrong. My connection which i am using in the Apache directory studio is secure connection .. Do i need to specify the LDAPS or ldap will work

public void GetAllADUsers( string username)
{
    try
    {

        DirectoryEntry myLdapConnection = new DirectoryEntry();
myLdapConnection.Path = "LDAP://server:port/o=value";
myLdapConnection.Username = @"CN=cn,OU=ou,OU=Users,O=o";
myLdapConnection.Password = "password";

//myLdapConnection.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ( "(&(objectClass=user)") };

     SearchResultCollection allResults = search.FindAll();
        if(allResults.Count == 0)
{

            Console.WriteLine("not found");
 }
else
{
Console.WriteLine ("found");
}
catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
 }

Note username if i specify = @"CN=cn,OU=ou,OU=Users,O=o"; myLdapConnection threw the DirectorySearcherCOMException but if only pass the username =cn means on cn value.. this gives all the values

exception

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)\r\n at System.DirectoryServices.DirectoryEntry.Bind()\r\n at System.DirectoryServices.DirectoryEntry.get_AdsObject()\r\n at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)\r\n at System.DirectoryServices.DirectorySearcher.FindAll()\r\n at _Default.GetAllADUsers(String username)

4

0 回答 0