2014-07-09 39 views
0

我们有一个很大的LDAP目录,我们目前正在返回所有用户。我们遍历用户列表,比较我们在本地保存的内容,找到不再存在的或新的内容,然后在本地创建/删除它们。搜索DirectoryServices以按日期返回修改的用户列表

问题是此操作需要小时完成。

我认为解决方案是为目录服务定义一个更具体的搜索查询,并且只返回那些在过去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. 
      } 

回答

0

这是通过使用这些LDAP过滤器属性解决:

修改:2014年7月10日上午08点35分十七秒

modifyTimeStamp:2014年7月10日上午08点35分十七秒

按照this post的说明找到了。

+0

- >我有几乎相似的要求。这是一个旧的帖子,但你可以张贴一些样本如何使用C#AD查询来检索自日期以来修改的项目?另外,它是否也会返回已删除的项目? – user2107373

+0

该查询是基于您要使用的属性过滤器创建的。类似于'dirSearcher.Filter =“(&(PROPERTYNAME =”+ VALUE +“))”;'。将PROPERTYNAME替换为实际的AD属性,然后显示您提供的值。阅读https://msdn.microsoft.com/en-us/library/aa746475(v=vs.85).aspx –

0

你应该使用LDAP过滤器,并找到了几个examples很容易。我不确定日期的过滤器语法。我会检查documentation

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

+0

您提供的文档链接没有提供任何我需要的示例:( –

+0

该示例的情况如何?您是否尝试使用DirectorySearcher? – ziya

+0

我搜索LDAP筛选器查询的次数越多,我越意识到官方文档有多糟糕 –