Se si sta utilizzando .NET 3.5 o più recente, è possibile utilizzare un PrincipalSearcher
e un "Query-by-example" principale per fare la tua ricerca:
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "Esteban*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
Se non avete 's già - assolutamente leggere l'articolo MSDN Managing Directory Security Principals in the .NET Framework 3.5 che mostra bene come sfruttare al meglio le nuove funzionalità in System.DirectoryServices.AccountManagement
. Oppure vedi lo spazio dei nomi MSDN documentation on the System.DirectoryServices.AccountManagement.
Naturalmente, a seconda delle necessità, si potrebbe voler specificare altre proprietà su quella principale utente "di query-by-example" si crea:
DisplayName
(tipicamente: nome + spazio + cognome)
SAM Account Name
- Windows/AD nome dell'account
User Principal Name
- il vostro "[email protected]" nome dello stile
È possibile spe cificare una delle proprietà su UserPrincipal
e utilizzarle come "esempio per query" per il proprio PrincipalSearcher
.
fonte
2012-03-09 13:54:26
Sto solo mantenendo un codice esistente, quindi non voglio rovinarmi molto con il codice che mi hai mandato, tuttavia con questo suppongo di poterlo inserire all'inizio e alla fine e dovrebbe funzionare anche. –
come questo: sAMAccountName = * "+ sAMAccountName +" *) –
@EstebanV .: [in base a questo] (http://technet.microsoft.com/en-us/library/ee198823.aspx) sembra che dovrebbe funzionare , sì. –