有没有办法使用PrincipalSearcher搜索所有不匹配的记录?使用DirectorySearcher,您可以应用像(!sn="\*Jay\*")这样的过滤器。换句话说,姓氏中任何位置都不包含序列"Jay“的所有记录。我想知道是否有任何方法可以用UserPrincipal参数来做这件事。
发布于 2013-01-14 05:48:50
不幸的是,这不是一个选项。我花了相当多的时间试图找到一种方法来轻松/高效地进行更高级的搜索。最接近高级搜索的是一些日期选项,但没有文本搜索。
我最终所做的是使用一个LDAP查询单独运行一个DirectorySearcher。我从搜索中返回的唯一属性(以最小化结果集大小和提高速度)是DN和对象类型(如果对象类型尚未过滤)。然后,我使用DN创建一个适当类型的新的Principal对象,并将其添加到集合中。
整个AccountManagement库在设计时只考虑了非常小的一组任务(显然),并且很难扩展。对于大多数任务,我已经退回到使用DirectoryServices,因为它才是真正应该使用的库。
发布于 2015-06-08 23:39:17
我似乎总是在回答老问题,但在搜索中不断出现这个问题,我相信我已经解决了这个问题。如果它能帮助找到它的人,这里是我设法拼凑起来的东西。
您要做的是创建一个自定义的AdvancedFilter,并根据您的需要创建一些自定义的搜索规则。默认的是相当有限的,但是你可以创建任何你需要的东西。
你的问题的一个例子可以这样处理
public class MyAdvancedFilter : AdvancedFilters
{
public MyAdvancedFilter(Principal principal) : base(principal)
{
}
public void WhereLastName(string lastName, MatchType match)
{
// The * is the normal wildcard for LDAP.
// Remove *'s for an exact match, or create a parameter to choose what to do.
this.AdvancedFilterSet("sn", "*" + lastName + "*", typeof(string), match);
}
}为了使用它,您还必须有一个实现它的自定义UserPrincipal对象。
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
public class MyUser : UserPrincipal
{
public MyUser(PrincipalContext context) : base(context)
{
}
private MyAdvancedFilter searchFilter;
// use custom search filters
public new MyAdvancedFilter AdvancedSearchFilter
{
get
{
if (searchFilter == null)
searchFilter = new MyAdvancedFilter(this);
return searchFilter;
}
}
}现在,您已经准备好使用此代码。
MyUser u = new MyUser();
// find users without the last name containing "Jay"
u.AdvancedSearchFilter.WhereLastName("Jay", MatchType.NotEquals);
PrincipalSearcher ps = new PrincipalSearcher(u);
var res = ps.FindAll().Cast<MyUser>();
foreach (MyUser p in res)
{
// use the results here.
}希望这能帮助到别人。
https://stackoverflow.com/questions/13483046
复制相似问题