首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用System.DirectoryServices.AccountManagement在多个域名中搜索?

问如何使用System.DirectoryServices.AccountManagement在多个域名中搜索?
EN

Stack Overflow用户
提问于 2012-05-05 14:27:41
回答 3查看 20.9K关注 0票数 15

我有三个或更多的域名,如main.com,sub.main.com,sub2.main.com等

我有一个代码:

代码语言:javascript
复制
using (PrincipalContext ctx = 
    new PrincipalContext(ContextType.Domain, "ADServer", 
    "dc=main,dc=com", ContextOptions.Negotiate))
{
    UserPrincipal u = new UserPrincipal(ctx);
    u.UserPrincipalName = "*" + mask + "*";

    using (PrincipalSearcher ps = new PrincipalSearcher(u))
    {
       PrincipalSearchResult<Principal> results = ps.FindAll();
       List<ADUser> lst = new List<ADUser>();

       foreach (var item in results.Cast<UserPrincipal>().Take(15))
       {
           byte[] sid = new byte[item.Sid.BinaryLength];
           item.Sid.GetBinaryForm(sid, 0);

           ADUser us = new ADUser()
           {
               Sid = sid,
               Account = item.SamAccountName,
               FullName = item.DisplayName
           };

           lst.Add(us);
       }

    }

    return lst;
}

但它只在一个域中进行搜索:main.com。

如何一次搜索所有域中的记录?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-10 12:05:47

下面是一种从根域名中查找所有域名的方法:

代码语言:javascript
复制
/* Retreiving RootDSE
 */
string ldapBase = "LDAP://DC_DNS_NAME:389/";
string sFromWhere = ldapBase + "rootDSE";
DirectoryEntry root = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD");
string configurationNamingContext = root.Properties["configurationNamingContext"][0].ToString();

/* Retreiving the root of all the domains
 */
sFromWhere = ldapBase + configurationNamingContext;
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD");

DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(&(objectClass=crossRef)(nETBIOSName=*))";
dsLookForDomain.SearchScope = SearchScope.Subtree;
dsLookForDomain.PropertiesToLoad.Add("nCName");
dsLookForDomain.PropertiesToLoad.Add("dnsRoot");

SearchResultCollection srcDomains = dsLookForDomain.FindAll();

foreach (SearchResult aSRDomain in srcDomains)
{
}

然后,在每个域中,您可以查找所需的内容。

票数 13
EN

Stack Overflow用户

发布于 2013-05-22 15:15:17

您应该使用GC而不是LDAP。它沿着整个域森林进行搜索

代码语言:javascript
复制
var path = "GC://DC=main,DC=com";

try
{
    using (var root = new DirectoryEntry(path, username, password))
    {
        var searchFilter = string.Format("(&(anr={0})(objectCategory=user)(objectClass=user))", mask);
        using (var searcher = new DirectorySearcher(root, searchFilter, new[] { "objectSid", "userPrincipalName" }))
        {
            var results = searcher.FindAll();
            foreach (SearchResult item in results)
            {
                //What ever you do
            }
        }
    }
}

catch (DirectoryServicesCOMException)
{
    // username or password are wrong
}
票数 26
EN

Stack Overflow用户

发布于 2015-03-21 01:18:26

要实际使用System.DirectoryServices.AccountManagement执行搜索,请按如下方式指定域:

代码语言:javascript
复制
new PrincipalContext(ContextType.Domain, "xyz.mycorp.com:3268", "DC=mycorp,DC=com");

来自When do I need a Domain Name and a Domain Container to create a PrincipalContext?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10459477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档