通过用户名搜索整个Active Directory林 [英] Search Entire Active Directory Forest by username

查看:107
本文介绍了通过用户名搜索整个Active Directory林的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组织拥有由几个域名组成的活动目录林.我需要编写一个应用程序以通过用户ID查找用户.

My organization has active directory forestry consisting of several domain names. I need to write an application to find a user by user id.

        string username = "test_user_id";

        DirectoryEntry entry = new DirectoryEntry("LDAP://one_of_the_domain");
        DirectorySearcher dSearch = new DirectorySearcher(entry);
        dSearch.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "))";
        SearchResult result = dSearch.FindOne();

        if (result != null)
        {
            var email = result.Properties["mail"];
            Console.WriteLine(email[0]);
        }

上面的示例代码将允许我在one_of_the_domain罚款范围内搜索用户.但是有什么方法可以在整个活动目录林中找到用户?

The sample code above will allow me to search user within one_of_the_domain fine. But is there a way I can find users within entire active directory forest?

推荐答案

使用Forest类获取当前的全局目录,然后在其中可以获取对DirectorySearcher的引用,该目录将搜索整个目录林.

Use the Forest class to get the current global catalog, where you then can get a reference to a DirectorySearcher that will search the entire forest.

    var currentForest = Forest.GetCurrentForest();
    var gc = currentForest.FindGlobalCatalog();

    using (var userSearcher = gc.GetDirectorySearcher())
    {
      userSearcher.Filter = 
"(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "))";
            SearchResult result = userSearcher.FindOne();

    }

这篇关于通过用户名搜索整个Active Directory林的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆