Si estás en .NET 3.5 o posterior, que debería echa un vistazo a la PrincipalSearcher
y un director "de consulta por ejemplo" para hacer su búsqueda:
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=SomeOU,DC=YourCompany,DC=com");
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// set the PageSize on the underlying DirectorySearcher to get all 3000 entries
((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
Si aún no lo ha - leer el artículo de MSDN absolutamente Managing Directory Security Principals in the .NET Framework 3.5 que muestra muy bien cómo hacer el mejor uso de la nuevas funciones en System.DirectoryServices.AccountManagement
Actualización:
Por supuesto, dependiendo de sus necesidades, es posible que desee especificar otras propiedades en ese principal "de consulta por ejemplo" usuario se crea:
Surname
(o último nombre)
DisplayName
(normalmente: nombre + espacio + apellido)
SAM Account Name
- su/AD nombre de cuenta de Windows
User Principal Name
- su "[email protected]" nombre de estilo
Puede especificar cualquiera de las propiedades en el UserPrincipal
y utilizar los como "consulta por ejemplo" para su PrincipalSearcher
.
Actualización n. ° 2: Si desea buscar dentro de una unidad organizativa determinada, puede definir esa unidad organizativa en el constructor de la PrincipalContext
.
pero quiero filtrar por unidad organizativa, y obtener un resultado de todo tipo es usuario en esa unidad organizativa, aquí no hay palabra clave para buscar – cciikk
¡Es genial, gracias! – cciikk
@cciikk: vea mi actualización: puede limitar su búsqueda a un contenedor específico definiéndolo en el constructor del 'PrincipalContext' –