从Active Directory检索用户名

时间:2013-05-03 18:24:31

标签: c# active-directory

我无疑是AD的新手。我有一个下拉列表,我已经绑定了组织内的成员列表。我的最终目标是找到他们的经理名字,但我从小步骤开始。

我已经做了足够的搜索以获得正确的结果。我在使用结果

获取正确数据(通过使用断点等进行验证)时遇到问题
private void cmbUserList_SelectedIndexChanged(object sender, EventArgs e)
{
    var userName = cmbUserList.SelectedValue.ToString();
    DirectorySearcher search = new DirectorySearcher();

    search.Filter = String.Format("(cn={0})", userName);
    search.PropertiesToLoad.Add("givenName");

    SearchResult result = search.FindOne();

    if (result != null)
    {
        // For now I'm trying to just retrieve their name
        lblManagerName.Text = result.GetDirectoryEntry().Name;
    }
}

编辑:我正在使用.net 4.0版

是否有人指出我要检索正确的名称,然后甚至可能是链接或资源来提取经理名称?

3 个答案:

答案 0 :(得分:2)

我认为您的代码存在的问题是您正在使用“(cn = {0})”,userName。您需要传递完全限定的名称,如

CN=Doe,John,OU=Users,OU=Headquarters,DC=company,DC=net

如果您只有登录ID,则下面的代码应该可以使用

DirectorySearcher directorySearcher = new DirectorySearcher("LDAP://RootDSE");
directorySearcher.Filter = "sAMAccountName=" + acctName;
directorySearcher.PropertiesToLoad.Add("manager");
SearchResult searchResult = directorySearcher.FindOne();
if (searchResult != null)
DirectoryEntry user = searchResult.GetDirectoryEntry();

请注意,acctName是Windows登录ID。如果您想使用AD并检查各种属性及其存储方式,请尝试使用dsquery和dsget命令行工具。以下命令将根据登录ID返回用户记录,并显示manager字段的内容:

dsquery user domainroot -samid "loginid" | dsget user -samid -mgr

答案 1 :(得分:0)

助手类和枚举

public enum ActiveDirectoryObjectClass
{
    Computer,
    User,
    Domain,
    Group,
}

public static class ActiveDirectorySearcher
{
    public static string  GetCurrentDomainName()
    {
        string result;
        using (Domain domain = Domain.GetCurrentDomain())
        {
            result = domain.Name;
        }

        return result;
    }

    public static IEnumerable<T> Select<T>(
        ActiveDirectoryObjectClass activeDirectoryObjectClass,
        Func<DirectoryEntry, ActiveDirectoryObjectClass, bool> condition,
        Func<DirectoryEntry, T> selector
        )
    {
        List<T> list = new List<T>();
        using (Domain domain = Domain.GetCurrentDomain())
        using (DirectoryEntry root = domain.GetDirectoryEntry())
        {
            string filter = string.Format("(objectClass={0})", activeDirectoryObjectClass);
            using (DirectorySearcher searcher = new DirectorySearcher(filter))
            {
                searcher.SearchRoot = root;
                searcher.SearchScope = SearchScope.Subtree;
                using (SearchResultCollection result = searcher.FindAll())
                {
                    foreach (SearchResult item in result)
                    {
                        using (DirectoryEntry entry = item.GetDirectoryEntry())
                        {
                            if (condition(entry, activeDirectoryObjectClass))
                            {
                                list.Add(selector(entry));
                            }
                        }
                    }
                }
            }

        }
        return list;
    }
}

如何使用

    public IEnumerable<User> GetUsers()
    {
        return ActiveDirectorySearcher.Select(
            ActiveDirectoryObjectClass.User,
            (entry, adObjectClass) => string.Compare(entry.SchemaClassName, adObjectClass.ToString(), StringComparison.InvariantCultureIgnoreCase) == 0,
            _ => new User
                     {
                         Name = _.Name.Substring(3),
                         Domain = ActiveDirectorySearcher.GetCurrentDomainName(),
                     });
    }

注意:示例中的用户 - 具有属性Name,Domain等的自定义类

答案 2 :(得分:0)

查找姓名和/或经理姓名:

if (sResult != null)
{
    string userName = sResult.Properties["name"][0].ToString();
    string managerDN = sResult.Properties["manager"][0].ToString();

    DirectoryEntry man =  new DirectoryEntry("LDAP://server_name/"+managerDN);
    string managerName = man.Properties["name"][0].ToString();

}

server_name可以只是FQDN的域组件,即yourcompany.com,这样它就可以通过DNS找到目录服务器。

编辑:

我还从Sysinternals推荐了Active Directory Explorer。它是探索和理解AD结构的好工具