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 に答える