C# 如何根据姓氏和名字在 Active Directory 中搜索用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9603878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I search users in Active Directory based on surname and first name?
提问by bAN
I'm trying to search for users in AD with their surname (sn) and first name (givenName) using DirectorySearcherin .NET.
我正在尝试使用.NET 中的姓氏 ( sn) 和名字 ( givenName)在 AD 中搜索用户DirectorySearcher。
I can find a user based on sAMAccountnamewith this code:
我可以找到基于sAMAccountname此代码的用户:
DirectorySearcher searcher1 = new DirectorySearcher(entry);
searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(SAMAccountname={0}))",aLogin);
SearchResult results1;
results1 = searcher1.FindOne();
But when I try to do it with givenNameand sn:
但是当我尝试使用givenNameand来做到这一点时sn:
DirectorySearcher searcher1 = new DirectorySearcher(entry);
searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1})", aName, aSName);
SearchResultCollection results1;
results1 = searcher1.FindAll();
It doesn't work; the message says "Invalid Filter";
Can I not filter based on givenNameand sn?
它不起作用;消息显示“无效过滤器”;我不能基于givenName和过滤sn吗?
How can I achieve this?
我怎样才能做到这一点?
采纳答案by Matt
You're missing a closing parentheses in your filter. Try:
您的过滤器中缺少右括号。尝试:
searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", aName, aSName);
回答by bAN
No way this is an error..
不可能这是一个错误..
I forgot a )
我忘了一个 )
回答by marc_s
If you're using .NET 3.5 or newer, you could also make use of the PrincipalSearcherand a "query-by-example" principal to do your searching:
如果您使用 .NET 3.5 或更新版本,您还可以使用PrincipalSearcher和“query-by-example”主体来进行搜索:
// 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" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
qbeUser.Surname = "Miller";
// 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.....
}
If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagementnamespace.
如果您还没有阅读过,请务必阅读 MSDN 文章在 .NET Framework 3.5 中管理目录安全主体,该文章很好地展示了如何充分利用System.DirectoryServices.AccountManagement. 或者查看有关 System.DirectoryServices.AccountManagement命名空间的MSDN 文档。
Of course, depending on your need, you might want to specify other properties on that "query-by-example" user principal you create:
当然,根据您的需要,您可能希望在您创建的“query-by-example”用户主体上指定其他属性:
DisplayName(typically: first name + space + last name)SAM Account Name- your Windows/AD account nameUser Principal Name- your "[email protected]" style name
DisplayName(通常:名字 + 空格 + 姓氏)SAM Account Name- 您的 Windows/AD 帐户名User Principal Name- 您的“[email protected]”样式名称
You can specify any of the properties on the UserPrincipaland use those as "query-by-example" for your PrincipalSearcher.
您可以在 上指定任何属性,UserPrincipal并将它们用作PrincipalSearcher.

