1

我添加了自定义属性lastLogonTime语法:UTC Coded Time. 我将UserPrincipal类扩展为 GET/SET 该自定义属性。

在此处输入图像描述

...
[DirectoryProperty("lastLogonTime")]
public DateTime? LastLogonTime
{
   get
   {
      object[] result = this.ExtensionGet("lastLogonTime");
      if (result != null && result.Count() > 0) return (DateTime?)result[0];
           return null;
   }
   set
   {
      this.ExtensionSet("lastLogonTime", value);
   }
}
...

我还扩展AdvancedFilters为能够通过此自定义属性进行搜索。

MyUserPrincipalSearch searchFilter;

new public MyUserPrincipalSearch AdvancedSearchFilter
{
   get
   {
      if (null == searchFilter)
          searchFilter = new MyUserPrincipalSearch(this);
      return searchFilter;
   }
}

public class MyUserPrincipalSearch: AdvancedFilters
{
   public MyUserPrincipalSearch(Principal p) : base(p) { }
   public void LastLogonTime (DateTime? lastLogonTime, MatchType mt)
   {
     this.AdvancedFilterSet("lastLogonTime", lastLogonTime.Value, typeof(DateTime?), mt);
   }
}

现在,我想搜索所有lastLogonTime小于 day.

using (PrincipalContext ctx = ADLDSUtility.Users)
{
   MyUserPrincipal filter = new MyUserPrincipal(ctx);
   filter.AdvancedSearchFilter.LastLogonTime((DateTime.Now - new TimeSpan(1,0,0,0,0)), MatchType.LessThan);
   PrincipalSearcher ps = new PrincipalSearcher(filter);
   foreach (MyUserPrincipal p in ps.FindAll())
   {
      //my custom code
   }
}

上面的搜索没有返回任何结果。我有最近几天没有登录的测试用户。

我试过了MatchType.GreaterThanMatchType.Equals。他们都没有返回任何结果,但有些用户符合这些条件。

唯一有效的过滤器是

filter.AdvancedSearchFilter.LastLogonTime(DateTime.Now , MatchType.NotEquals);

但这基本上是返回所有用户。任何想法为什么我的搜索结果没有返回任何结果?

我的目标是搜索所有上次登录时间少于X几天的用户。

只要我得到这些用户,我就对其他解决方案持开放态度。

PS我确实知道解决这个问题的方法。遍历所有用户,获取他们的信息lastLogonTime,然后进行比较,但这对我正在做的事情来说太过分了。

4

1 回答 1

0

在这个问题上花了一些时间之后,我发现了问题。

正如我在帖子中提到的,自定义属性lastLogonTime具有语法:UTC Coded Time. 但是,日期和时间不会存储为DateTime. 它实际上string是以这种格式存储的:

yyyyMMddHHmmss.0Z

我最终解决这个问题的方法是AdvancedSearchFilter.LastLogonTime使用格式化字符串修改我的搜索。

public void LastLogonTime (DateTime? lastLogonTime, MatchType mt)
{
   const string lastLogonTimeFormat = "yyyyMMddHHmmss.0Z";
   this.AdvancedFilterSet("lastLogonTime", lastLogonTime.Value.ToUniversalTime().ToString(lastLogonTimeFormat), typeof(string), mt);
}

希望这可以帮助某人。

于 2015-08-15T04:15:38.087 回答