none
Get UserList From Remote Active Directory

    Question

  • Hello,

    Is there any way I can get user list from remote active directory server using C# ?

    Tuesday, April 4, 2017 6:28 AM

All replies

  • Hi Tech Aspirant,

    Thank you for posting here.

    For your question, you could try the following code. It works for me. It will get all the user in Domain for your reference.

      public static void GetAD()
            {
                List<string> userNameList = new List<string>();
                using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
                {
                    using (UserPrincipal user = new UserPrincipal(context))
                    {
                        using (PrincipalSearcher searcher = new PrincipalSearcher(user))
                        {
                            foreach (System.DirectoryServices.AccountManagement.UserPrincipal result in searcher.FindAll())
                            {
                                userNameList.Add(result.Name);//put all the users in the list
                                Console.WriteLine(result.Name);//or you could output all the user name
                            }
    
                        }
                    }
                }
            }

    It is a simple way to get all the user in same domain.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 5, 2017 6:00 AM
    Moderator