我尝试从Active Directory中的对象获取所有属性的列表.
我现在拥有的是:
List<User> users = new List<User>();
try
{
DirectoryEntry root = new DirectoryEntry("LDAP://RootDSE");
root = new DirectoryEntry("LDAP://" + root.Properties["defaultNamingContext"][0]);
DirectorySearcher search = new DirectorySearcher(root);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("title");
SearchResultCollection results = search.FindAll();
if (results != null)
{
foreach (SearchResult result in results)
{
foreach (DictionaryEntry property in result.Properties)
{
Debug.Write(property.Key + ": ");
foreach (var val in (property.Value as ResultPropertyValueCollection)) {
Debug.Write(val +"; ");
}
Debug.WriteLine("");
}
}
}
}
catch (Exception ex) …Run Code Online (Sandbox Code Playgroud) 我们的应用程序有一个从 Active Directory 获取所有用户并使用他们的信息更新相关 SQL 表的过程。晚上的过程,它是几年前写的——所以它是遗留代码,并且“如果它没有损坏,就不要修复它”。但是,我们正在向我们的应用程序引入一项新功能,该功能需要修改此代码,并且由于多年未触及它,我想我不妨稍微清理一下。
所述进程仅在夜间运行,除非出现罕见的服务器故障,在这种情况下,我们必须在白天手动运行它。该过程使用良好的旧System.DirectoryServices库来完成它的工作,虽然它可以工作,但运行速度很慢。
我想改用较新的System.DirectoryServices.AccountManagement库,所以我开始重写整个过程(几百行代码),我惊讶地看到它的性能PrincipalSearcher 显着优于DirectorySearcher.
我一直在试图寻找原因并找到了以下 SO 答案,该答案对两者进行了比较,指出DirectorySearcher应该比PrincipalSearcher.
我启动了一个测试项目以确保我没有出现幻觉:
class Program
{
static void Main(string[] args)
{
// New stuff
var context = new PrincipalContext(ContextType.Domain, "mydomain.com");
var properties = new[] { "cn", "name", "distinguishedname", "surname", "title", "displayname" };
var i = 0;
var now = DateTime.Now;
new Thread(delegate()
{
while (true)
{
Console.Write("\r{0} ms, {1} results", (DateTime.Now - now).TotalMilliseconds, i); …Run Code Online (Sandbox Code Playgroud)