Active Directory Logon - Inappropriate DirectoryEntry Exception

I need to validate an LDAP user by checking if such a username exists 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 that I would like to reference and use if I need this functionality in my project.

To test this, I created a simple test application. The test proceeds as follows:

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

      

The problem I am running into is that this works great when I test it with testapp, but in my project when I try to use the same method with the same credentials. The object DirectoryEntry

throws an exception "System.DirectoryServices.DirectoryServicesCOMException"

and search.Filter

fails and throws an exception ex = {"Login failed: unknown username or invalid password. \ R \ n"}.

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

+2


a source to share


2 answers


I tried almost every possible solution I could find in every such thread, but I still couldn't solve it.

I tried to redo the whole thing and then it worked. I think the reason it was working with my test app and not my project is because my project was saved to a network location and the test app was saved to my PC hard drive.



It started working fine with my project when I copied my project to my hard drive on my PC. My best guess is that since the project was located on the network, there might not have been sufficient permissions to validate LDAP.

+1


a source


Are you sure your test application and your real application are running as the same user, so they have the same permissions in AD? Although the exceptions thrown by DirectoryEntry objects are tricky to troubleshoot, that's the first thing I checked. Is this a web application or a desktop application? If it is a web application, then the user running the application pool may not have the required permissions.



0


a source







All Articles