首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中使用全名搜索ActiveDirectory?

在C#中使用全名搜索ActiveDirectory?
EN

Stack Overflow用户
提问于 2011-07-06 00:16:37
回答 1查看 7.2K关注 0票数 9

UserPrincipalFindByIdentity方法允许我使用用户名搜索ActiveDirectory。但是,我也希望能够使用用户的真实姓名(例如,韦恩,布鲁斯)进行搜索。

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-06 00:42:02

您可以使用PrincipalSearcher和“按示例查询”主体来执行搜索:

代码语言:javascript
复制
// 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 = "Bruce";

// 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文章Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement中的新特性

更新:

当然,根据您的需要,您可能希望在您创建的“按示例查询”用户主体上指定其他属性:

  • Surname (或姓氏name)
  • DisplayName (通常为:名字+空格+姓氏name)
  • SAM Account Name -您的typically/AD帐户name
  • User Principal Name -您的"username@yourcompany.com“样式名称

您可以指定UserPrincipal上的任何属性,并将这些属性用作PrincipalSearcher的“按示例查询”。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6585747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档