I need a routine to randomly select records from ADAM (Active Directory Application Mode). Any advice to get me started on this task?
A:
Use a DirectorySearcher filter with (objectClass=user) and pick a result at random might work. Something like ...
private static Random rnd = new Random();
private static DirectoryEntry GetRandomUser()
{
DirectoryEntry luckyGuy;
var de = new DirectoryEntry(/*conn string*/);
de.Username = /* your user */;
de.Password = /* your pass */;
// error handling and try-catch removed for clarity and brevity
var s = new DirectorySearcher( de );
s.Filter = "(objectClass=user)";
var res = s.FindAll();
if( res.Count > 0 )
{
var idex = rnd.Next(0, res.Count);
luckyGuy = res[idex].GetDirectoryEntry();
}
return luckyGuy;
}
Here's more on DirectorySearcher.
JP Alioto
2009-06-30 19:26:51
How would you randomly pick a result from the collection?
JD
2009-06-30 21:20:26