从Active Directory应用程序模式中随机选择一条记录

时间:2009-06-30 19:13:22

标签: algorithm directoryservices

我需要一个例程来从ADAM(Active Directory应用程序模式)中随机选择记录。有什么建议让我开始这个任务吗?

1 个答案:

答案 0 :(得分:1)

使用DirectorySearcher过滤器(objectClass = user)并随机选择结果可能有效。有点像...

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;
}

这是more on DirectorySearcher