我添加了自定义属性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.GreaterThan
,MatchType.Equals
。他们都没有返回任何结果,但有些用户符合这些条件。
唯一有效的过滤器是
filter.AdvancedSearchFilter.LastLogonTime(DateTime.Now , MatchType.NotEquals);
但这基本上是返回所有用户。任何想法为什么我的搜索结果没有返回任何结果?
我的目标是搜索所有上次登录时间少于X
几天的用户。
只要我得到这些用户,我就对其他解决方案持开放态度。
PS我确实知道解决这个问题的方法。遍历所有用户,获取他们的信息lastLogonTime
,然后进行比较,但这对我正在做的事情来说太过分了。