Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public static bool ValidateActiveDirectoryLogin(string Domain, string Username, string Password)
{
	bool isValid = false;
	try
	{
		string ipAddress = "";
		if (Dns.GetHostAddresses(Dns.GetHostName()).Length > 0)
		{
			ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
		}

		//create a "principal context" - e.g. your domain (could be machine, too)
		//using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
		using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain, "DC=estagioit,DC=local"))
		{
			// validate the credentials
			isValid = pc.ValidateCredentials(Username, Password);
		}
	}
	catch (Exception ex)
	{
		isValid = false;
	}
	return isValid;

}


What I have tried:

public static bool ValidateActiveDirectoryLogin(string Domain, string Username, string Password)
{
	bool isValid = false;
	try
	{
		string ipAddress = "";
		if (Dns.GetHostAddresses(Dns.GetHostName()).Length > 0)
		{
			ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
		}
		using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain, "DC=estagioit,DC=local"))
		{
			isValid = pc.ValidateCredentials(Username, Password);
		}
	}
	catch (Exception ex)
	{
		isValid = false;
	}
	return isValid;

}
Posted
Updated 28-Aug-20 8:36am
v3
Comments
F-ES Sitecore 20-Feb-19 5:02am    
There's obviously an issue with your client machine connecting to the target server. We can't inspect your network and we can't run your code in the context of your local machine so there's not much we can do to help.
Member 13944608 20-Feb-19 5:19am    
ok thanks
Richard Deeming 20-Feb-19 7:46am    
string ipAddress = "";
if (Dns.GetHostAddresses(Dns.GetHostName()).Length > 0)
{
    ipAddress = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
}


That's two relatively expensive method calls, both called twice, to populate a variable which you never use. Was this meant to server a purpose other than slowing your code down?

When I've run into this problem it was always due to incorrectly specifying the server (ie. typo) or a firewall issue. Try dropping your firewall and see if it works.
 
Share this answer
 
v2
So, BTW, this error message ("The server could not be contacted.") is the default for System.DirectoryServices.AccountManagement.PrincipalServerDownException. When you have established code running and it stops with this message in your logs - something on the network has changed.
 
Share this answer
 

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