如何从名字或姓氏(从一个输入文本框)获取AD用户名列表
本文关键字:文本 输入 AD 列表 用户 一个 获取 | 更新日期: 2023-09-27 18:35:19
场景:用户在文本框中输入名称(可以是名字或姓氏),然后单击搜索按钮。系统应返回名字或姓氏与现有 AD 用户匹配的所有用户名(以及全名)。
问题:输入文本不会同时针对名字和姓氏进行检查。
List<string> GetUserDetails()
{
List<string> allUsers = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "myDomain",
"OU=ounit,dc=myDC,dc=com");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = _UITxtUserName.Text;
qbeUser.Surname = _UITxtUserName.Text;
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
foreach (var found in srch.FindAll())
{
allUsers.Add(found.DisplayName +"(" + found.SamAccountName+")");
}
allUsers.Sort();
return allUsers;
}
我可以看到问题出在_UITxtUserName(文本框)上。但不确定如何修复它。使用 .Net 3.5。
工作代码
List<string> GetUserDetails()
{
List<string> allUsers = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "myDomain",
"OU=ounit,dc=myDC,dc=com");
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = _UITxtUserName.Text;
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
foreach (var found in srch.FindAll())
{
allUsers.Add(found.DisplayName + "(" + found.SamAccountName + ")");
}
qbeUser = null;
qbeUser = new UserPrincipal(ctx);
qbeUser.Surname = _UITxtUserName.Text;
PrincipalSearcher srch1 = new PrincipalSearcher(qbeUser);
foreach (var found in srch1.FindAll())
{
allUsers.Add(found.DisplayName + "(" + found.SamAccountName + ")");
}
allUsers.Sort();
return allUsers;
}