搜索DirectoryServices以按日期返回已修改用户的列表

时间:2014-07-09 09:09:58

标签: c# ldap directoryservices

我们有一个大型LDAP目录,我们目前正在从中返回所有用户。我们遍历用户列表,并比较我们在本地保存的内容,以查找不再存在或新的内容,然后在本地创建/删除它们。

问题是此操作需要HOURS才能完成。

我认为解决方法是为目录服务定义更具体的搜索查询,并仅返回在过去24小时内(或上次运行时)修改过的用户。不幸的是,我很难找到使用哪个属性来使搜索查询更具体。

我看过this list of available properties,但我能看到的可能是'ms-DFS-Last-Modified-v2',但是,我不知道如何使用它。

还有其他想法吗?

我们目前用于搜索的代码如下:

            PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "MYDOMAIN", "dc=MYDOMAIN,dc=co,dc=za");
            UserPrincipal theuser = new UserPrincipal(domainContext);

            theuser.Name = "*";

            // create a principal searcher for running a search operation
            PrincipalSearcher pS = new PrincipalSearcher(theuser);

            // assign the query filter property for the principal object 
            pS.QueryFilter = theuser;

            // run the query
            PrincipalSearchResult<Principal> theresults = pS.FindAll();
            retUsers = new List<ActiveDirectoryUser>();
            List<UserPrincipal> copyUsers = new List<UserPrincipal>();
            copyUsers = theresults.OfType<UserPrincipal>().Where(userresult => userresult.EmailAddress != null).ToList();


            foreach (UserPrincipal result in copyUsers)
            {
               ... process users.
            }

2 个答案:

答案 0 :(得分:0)

您应该使用LDAP过滤器,并且很容易找到一些examples。我不确定日期的过滤器语法。我会检查documentation

编辑:您可以通过查询schema来获取属性列表。遗憾的是,文档中没有很多例子。有关示例,请查看questions about Active Directory attribute listings

答案 1 :(得分:0)

使用这些LDAP过滤器属性解决了这个问题:

修改时间:2014年7月10日上午8:35:17

modifyTimeStamp:7/10/2014 8:35:17

按照this post的说明找到了。