I have this method made by someone else and it works perfectly fine
The problem is that if I change the domain for something not even existing, the searcher is still finding a result for that username even with a wrong domain
public bool Validarcredenciales(string domain, ControlarSesiones objeto)//Metodo que valida si las credenciales son correctas.
{
string username = objeto.Usuario;
string pwd = objeto.Clave;
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
//Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry) { Filter = "(SAMAccountName=" + username + ")" };
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
MensajeError = Resources.ResourcesETB.ErrorCredenciales;
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
FilterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
MensajeError = Resources.ResourcesETB.ErrorCredenciales;
return false;
}
return true;
}