views:

167

answers:

2

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?

A: 

Are you sure your test application and your real application are running as the same user, so they have the same permissions in AD? Though exceptions thrown by DirectoryEntry objects are tough to troubleshoot, that's the first thing I'd check. What kind of a project is it - web app or desktop app? If it's a web app, then the user running the app pool may not have the necessary permissions.

rwmnau
I am using the same user in both my test and my project. The project is a web app. What ever the permissions the user has, if the same works in one app then it should in the other app right? Beats me..
Pavanred
A: 

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.

Pavanred
Are you using impersonnation, or current Windows user?
Will Marcouiller
I am using impersonation.
Pavanred