Click here to Skip to main content
15,503,491 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.

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