就像在ActiveDirectory中搜索 [英] Like search in ActiveDirectory

查看:127
本文介绍了就像在ActiveDirectory中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code在C#来轮询用户活动目录搜索LDAP:

I am searching LDAP using the following code in C# to poll active directory for users:

DirectoryEntry entry = new DirectoryEntry(ldapPath, userName, password);

DirectorySearcher Searcher = new DirectorySearcher(entry);

Searcher.CacheResults = true;
Searcher.SearchScope = SearchScope.Subtree;

Searcher.Filter = "(&(&(objectCategory=person)(objectClass=user))
    (|(samaccountname=" + userSearch.SamAccountName + "*)
    (&(GivenName=" + userSearch.FirstName + "*)(SN=" + userSearch.Surname + 
        "*))))";

Searcher.PropertiesToLoad.AddRange(new string[] {"DisplayName", "GivenName",
    "DistinguishedName","Title","manager",
         "mail", "physicalDeliveryOfficeName", "DirectReports", "Company", 
         "Description", "SAMAccountName"});

SearchResultCollection results = Searcher.FindAll();

List<ActiveUser> activeUsers = new List<ActiveUser>();

我跑它与输入参数userSearch.FirstName =乔和userSearch.LastName =基本法,并期待一个用户乔布洛格斯,但这并没有出现在结果列表。如果我试图用这个名字文本框在Active Directory用户和计算机在Windows工具,乔·布洛格斯出现在列表中的用户。我使用的是正确的LDAP路径。我使用了错误的过滤器复制在Windows工具的功能?是否有一个喜欢搜索显示名称?

I ran it with the input parameters userSearch.FirstName = "jo" and userSearch.LastName = "bl" and was expecting one user "Joe Bloggs", but this didn't appear in the result list. If I try this using the name textbox in Active Directory Users and Computers tool in Windows, Joe Bloggs appears as the only user in the list. I am using the correct LDAP path. Am I using the wrong filter to replicate the functionality in the windows tool? Is there a 'like' search on display name?

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

如果您使用的是.NET 3.5,你可以使用 PrincipalSearcher 和查询逐例如主要做你的搜索:

If you're on .NET 3.5 or up, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Jo*";
qbeUser.Surname = "Bl*";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果您还没有 - 绝对阅读MSDN文章管理目录安全主体在.NET Framework 3.5 这表明很好如何使新功能的最佳使用 System.DirectoryServices.AccountManagement

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

这篇关于就像在ActiveDirectory中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆