UserPrincipal的FindByIdentity方法允许我使用用户名搜索ActiveDirectory。但是,我也希望能够使用用户的真实姓名(例如,韦恩,布鲁斯)进行搜索。
我该怎么做呢?
发布于 2011-07-06 00:42:02
您可以使用PrincipalSearcher和“按示例查询”主体来执行搜索:
// 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帐户nameUser Principal Name -您的"username@yourcompany.com“样式名称您可以指定UserPrincipal上的任何属性,并将这些属性用作PrincipalSearcher的“按示例查询”。
https://stackoverflow.com/questions/6585747
复制相似问题