【问题标题】:How to get list of AD username from first name or surname (from one input textbox)如何从名字或姓氏(从一个输入文本框中)获取 AD 用户名列表
【发布时间】:2013-10-14 11:46:56
【问题描述】:

场景:用户在文本框中输入名称(可以是名字或姓氏)并单击搜索按钮。系统应返回与现有 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。

【问题讨论】:

  • 您不能这样做 - 现在,您的代码会检查具有 相同 名字和姓氏的用户,该名字和姓氏都与您的用户输入的任何内容相匹配。它是您极不可能拥有这样的用户。您需要先搜索名字的匹配项,然后在第二次搜索中查找姓氏的匹配项,然后加入两个结果列表。
  • 正如@marc_s 所说,通过将不同的过滤条件传递给 PrincipalSearcher 执行搜索 2 次。通过将 qbeUser 对象设置为 null,确保在调用第二次搜索之前清除过滤器。

标签: c# asp.net active-directory userprincipal


【解决方案1】:

工作代码

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;
    }

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2014-10-07
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多