Como verificar se um usuário existe no LDAP
Preciso verificar os usuários da empresa usando apenas o nome de usuário - e não a senha.
Então, eu preciso de um método como este
public bool UserExists(string username)
{ ... }
Eu estou ciente doSystem.DirectoryServices
namespace, mas não sabe por onde começar.
Alguma ideia?
Existem mais de 80.000 registros, então tente ter isso em mente.
Obrigado.
Editar:
Eu fiz isso - meu código é:
private bool UserExists(string userName, string domain)
{
try
{
DirectoryEntry.Exists("WinNT://" + domain + ".[hidden].com/" + userName);
return true;
}
catch (COMException)
{
return false;
}
}
Não sei se está correto, mas parece funcionar até agora.
A resposta de Michael tem duas partes relevantes:
http://www.codeproject.com/KB/system/everythingInAD.aspx#22http://www.codeproject.com/KB/system/everythingInAD.aspx#35Atualização # 2:
Na verdade, eu usei isso:
public static bool LoggedOnUserExists()
{
var domain = new PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, Environment.UserName);
return foundUser != null;
}