Click here to Skip to main content
15,171,439 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my code!

public static string ConnectAcitveDirectory(string groupName, string domainName, string serverName, string adminUserName, string queryUserName, string queryPassword)
               //DirectoryEntry connectionAD = new DirectoryEntry(serverName, queryUserName, queryPassword);
               //DirectorySearcher search = new DirectorySearcher(connectionAD);
               //search.PageSize = 1001;
               //search.Filter = "(&(objectClass=user)(SAMAccountName=" + adminUserName + "))";
               //SearchResultCollection result = search.FindAll();

               //if (result.Count > 0)
               //    foreach (SearchResult item in result)
               //    {
               //        if (item.Properties["SAMAccountName"].Count > 0
               //                && Convert.ToString(item.Properties["distinguishedName"][0]).Contains("OU=" + groupName))
               //            return "Active Directory is all set up! User was authenticated";
               //    }

               using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, serverName, domainName + "\\" + queryUserName, queryPassword))
                   using (UserPrincipal foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, adminUserName))
                       if (foundUser != null )
                           return "user found";
                           return "no user!;
           catch (Exception ex)
               return ex.ToString();

Both the above codes (commented and uncommented code) would work one at a time. They work in my local system, in a console application.

But if I move the console application's EXE file to a server, where Active Directory is installed, it would not work. Instead throws below error.
System.DirectoryServices.DirectoryServicesCOMException (0x8007052E): The user name or password is incorrect. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_AdsObject() at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) at tap.dom.usr.Authentication.ConnectAcitveDirectory(String groupName, String domainName, String serverName, String adminUserName, String queryUserName, String queryPassword)

Any idea guys? Please let me know if you have any solution for this.

Thank you,
Updated 11-Sep-20 0:27am
Herman<T>.Instance 25-Mar-15 8:53am
the message is pretty clear: The user name or password is incorrect.
Is the user known in the AD?
Which user connects?
RelicV 25-Mar-15 8:57am
is the question pretty clear?
I was able to connect to the AD server from my local machine.
a domain user credentials are provided.
Herman<T>.Instance 25-Mar-15 8:58am
and is the ISS user known?
RelicV 25-Mar-15 9:03am
I have a console application which doesn't require the ISS user. Isn't it?
The console application has a EXE file which will display if connected or if error. It shouldn't be worried about any user authentication/authorisation, if im correct.
RelicV 25-Mar-15 9:00am
Sorry, I shouldnt have answered to you rudely.
Yes, i have a domain user in the server and im providing his credentials in both the console/web apps.
They work in my local machine using IIS and Visual Studio. But this code is not functioning on server, where IIS and AD are installed.
ZurdoDev 25-Mar-15 9:08am
You have it tagged via I believe the app pool identity still needs to be an account that can call AD.
RelicV 25-Mar-15 9:30am
Once again, I repeat its a CONSOLE APPLCIATION.
ZurdoDev 25-Mar-15 9:54am
OK. But you may want to remove the ASP.Net tag on the question then.
Richard Deeming 25-Mar-15 9:18am
Is the server in the same AD domain as your computer?

What happens if you try passing null for the serverName parameter?
RelicV 25-Mar-15 9:31am
Yes, server in the same AD domain, but my computer is not the same domain.
I have not tried passing null for serverName parameter.
rohitmathurcom 26-Nov-15 4:07am
Did you got any solution for your problem pls share it with me , because we are facing the same issue
Member 3821526 13-Apr-16 3:12am
I have a similar problem. When I run the program manually it works. But when I put it in a scheduled task it fails. I even tried with a Windows service, but same problem.
Did you get your problem solved?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900