0

有没有办法使用 PrincipalSearcher 搜索所有不匹配的记录。使用 DirectorySearcher,您可以应用过滤器,例如(!sn="\*Jay\*"). 换句话说,所有姓氏不包含序列“Jay”的记录。我想知道是否有任何方法可以使用 UserPrincipal 参数。

4

2 回答 2

0

不幸的是,这不是一个选择。我花了相当多的时间试图找到一种方法来轻松/有效地进行更高级的搜索。最接近高级搜索的是一些日期选项,但没有文本搜索。

我最终做的是使用 LDAP 查询单独运行 DirectorySearcher。我从搜索中返回的唯一属性(以最小化结果集大小并提高速度)是 DN 和对象类型(如果尚未过滤对象类型)。然后,我使用 DN 创建适当类型的新 Principal 对象并将其添加到集合中。

整个 AccountManagement 库的设计考虑了一组非常小的任务(显然),并且很难扩展。对于大多数任务,我几乎放弃了使用 DirectoryServices,因为它是应该使用的真正库。

于 2013-01-13T21:48:49.637 回答
0

我似乎总是在回答老问题,但这一直在搜索中出现,我相信我已经弄清楚了。如果它可以帮助找到它的人,这就是我设法拼凑的。

您要做的是创建一个自定义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.
}

希望它可以帮助某人。

于 2015-06-08T15:39:16.670 回答