0

我需要连接到客户端 AD 服务器以显示所有用户的信息。他们给了我以下信息:fqdn、netbios 名称和域控制器。这足以连接吗?

using (var context = new PrincipalContext(ContextType.Domain, "",)) 
using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) 
{ 
   foreach (var result in searcher.FindAll()) 
   { 
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; 
   }
}

谢谢!

4

2 回答 2

1

我认为瑞恩向你展示了旧的方法。从您的代码看来,您正在使用较新的类。

            // create a principal searcher for running a search operation 
        using (PrincipalSearcher pS = new PrincipalSearcher(uParams))
        {
            // assign the query filter property for the principal object you created 
            // you can also pass the user principal in the PrincipalSearcher constructor 
            pS.QueryFilter = uParams;

            // run the query 
            using (PrincipalSearchResult<Principal> results = pS.FindAll())
            {
                foreach (Principal item in results)
                {
                    UserPrincipal u = item as UserPrincipal;
                    list.Add(new MyCustomClass(u.UserPrincipalName)
                    {
                        Cn = u.Name,
                        Email = u.EmailAddress,
                        EmployeeId = u.EmployeeId,
                        NameFirst = u.GivenName,
                        NameLast = u.Surname,
                        ObjectSid = u.Sid.ToString(),
                        DistinguishedName = u.DistinguishedName,
                        SamAccount = u.SamAccountName
                    });
                }
            }
        }

请注意,AD 仍然对您的查询施加了 1500 项限制,因此您可能需要将 DirectoryEntry 顶部发送到以下内容:

        /// <summary>
    /// group member enumeration, simple and fast for large AD groups
    /// </summary>
    /// <param name="deGroup"></param>
    /// <returns>list if distinguished names</returns>
    public static List<string> GetMemberList(DirectoryEntry deGroup)
    {
        List<string> list = new List<string>();
        DirectoryEntry entry = deGroup;

        uint rangeStep = 1000;
        uint rangeLow = 0;
        uint rangeHigh = rangeLow + (rangeStep - 1);
        bool lastQuery = false;
        bool quitLoop = false;

        do
        {
            string attributeWithRange;
            if (!lastQuery)
            {
                attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
            }
            else
            {
                attributeWithRange = String.Format("member;range={0}-*", rangeLow);
            }
            using (DirectorySearcher searcher = new DirectorySearcher(entry))
            {
                searcher.Filter = "(objectClass=*)";
                //searcher.Filter = LdapObjectMgr.filterDisabledUsers;

                searcher.PropertiesToLoad.Clear();
                searcher.PropertiesToLoad.Add(attributeWithRange);
                SearchResult results = searcher.FindOne();
                foreach (string res in results.Properties.PropertyNames)
                {
                    //list the property names
                    System.Diagnostics.Debug.WriteLine(res.ToString());
                }

                if (results.Properties.Contains(attributeWithRange))
                {
                    foreach (object obj in results.Properties[attributeWithRange])
                    {
                        //Console.WriteLine(obj.GetType());
                        if (obj.GetType().Equals(typeof(System.String)))
                        {
                        }
                        else if (obj.GetType().Equals(typeof(System.Int32)))
                        {
                        }
                        //Console.WriteLine(obj.ToString());
                        list.Add(obj.ToString());
                    }
                    if (lastQuery)
                    {
                        quitLoop = true;
                    }
                }
                else
                {
                    if (lastQuery == false)
                    { lastQuery = true; }
                    else
                    { quitLoop = true; }
                }
                if (!lastQuery)
                {
                    rangeLow = rangeHigh + 1;
                    rangeHigh = rangeLow + (rangeStep - 1);
                }
            }
        }
        while (!quitLoop);

        return list;
    }
于 2012-12-18T22:35:58.257 回答
0

要通过 C# 连接,您将需要以下内容:

DirectoryEntry child = new DirectoryEntry("LDAP://" + domainControllerName + "/" + 
        objectDn, userName, password);

如果您有域控制器名称、对象域、用户名和密码,那么您应该可以开始了。

只是提醒一下,你被否决了,因为你没有提到你之前尝试过的任何东西。

于 2012-12-18T22:04:13.127 回答