Search code examples
c#exceptionactive-directoryldapdirectoryentry

Active Directory login - DirectoryEntry inconsistent exception


I need to validate the LDAP user by checking if there exists such a user name in the specified domain. For this I am using this code -

DirectoryEntry entry = new DirectoryEntry("LDAP://" + strDomainController);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "SAMAccountName=" + strUserName;
SearchResult result = searcher.FindOne();
return (result != null) ? true : false;

This is a method in a class library which I intened to reference and use whereever I need this functionality in my project.

To test this, I created a simple test application. The test occurs like this -

Console.WriteLine(MyClassLib.MyValidateUserMethod("UserName", "Domain",ref strError).ToString());

The problem I am facing is that this works fine when I test it with my testapp but in my project, when I try to use the same method with the same credentials - The DirectoryEntry object throws an "System.DirectoryServices.DirectoryServicesCOMException" exception and the search.Filter fails and throws ex = {"Logon failure: unknown user name or bad password.\r\n"} exception.

I have tried impersonation but that doesn't help. Somehow the same method works fine in mytestapp and doesn't work in my project. Both these applications are in my local dev machine. What am I missing? Any ideas?


Solution

  • I tried almost every possible solution I could find on every such thread but I still could not resolve it.

    I tried to redo the entire thing and then it worked. I think, the reason that was responsible for it to work with my test app and not with my project is that my project was stored in a network location and my test app was stored in my PC's hard drive.

    It started working fine with my project when I copied my project on to my PC's hard drive. My best guess is that since the project was located on the network, perhaps there were not enough permissions granted for a LDAP validation.