Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to create a research for my listbox. I have created listbox called:
public void ExistUserList_SelectedIndexChanged(object sender, EventArgs e)
and a code that will write in a textbox
private void AllText2_TextChanged(object sender, EventArgs e)
which type is user and name of that user. Like for example "Guest" is a local user, "admin" is a local and domain user and "Romek" is a domain user.
This is my code in my listbox:
if (ExistUserList.SelectedItem != null)
            {
                selectedUsername = ExistUserList.SelectedItem.ToString();
            }
            
            if (ExistUserList.SelectedItem != null && ExistUserList.SelectedItem != "Administrator")
            {
                string selectedUsr = ExistUserList.SelectedItem.ToString();
                AllText2.Text = "Lokalny użytkownik: " + selectedUsr;
            }
            else if(ExistUserList.SelectedItem == "Administrator")
            {
                string selectedUsr = ExistUserList.SelectedItem.ToString();
                AllText2.Text = "Domenowy i lokalny użytkownik: " + selectedUsr;
            }

            // Dodatkowa logika dla domenowych użytkowników
            if (IsDomainUser(selectedUsername, SearchUsersOnServer()))
            {
                string allText = $"Domenowy użytkownik: {selectedUsername}";
                AllText2.Text = allText;
            }


And this is my code that download domain users:
private string selectedDomainUser;

        private void LoadDomainUsers()
        {
            try
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                UserPrincipal userPrincipal = new UserPrincipal(ctx);
                PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal);

                foreach (UserPrincipal result in searcher.FindAll())
                {
                    ExistUserList.Items.Add(result.SamAccountName);
                }
            }
            catch (Exception ex)
            {
                // Obsługa ewentualnych błędów
                MessageBox.Show("Wystąpił błąd: " + ex.Message);
            }


        }

        private List<string> SearchUsersOnServer()
        {
            List<string> domainUsers = new List<string>();

            // Łączenie z serwerem domenowym
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + sqlServerName);

            // Ustawienie filtra wyszukiwania na użytkowników
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.Filter = "(&(objectCategory=user)(objectClass=user)(name=*))";

            try
            {
                // Wykonanie wyszukiwania
                SearchResultCollection results = searcher.FindAll();

                // Wyczyszczenie istniejących wyników w listBox
                ExistUserList.Items.Clear();

                // Dodanie znalezionych użytkowników do listy
                foreach (SearchResult result in results)
                {
                    string domainusername = result.Properties["name"][0].ToString();
                    domainUsers.Add(domainusername);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Błąd podczas wyszukiwania użytkowników: " + ex.Message);
            }



            try
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

                UserPrincipal userPrincipal = new UserPrincipal(ctx);
                PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);

                ExistUserList.Items.Clear(); // Wyczyszczenie istniejących wyników w ListBox

                foreach (UserPrincipal result in principalSearcher.FindAll())
                {
                    string domainUsername = result.SamAccountName;
                    ExistUserList.Items.Add(domainUsername);

                    // Zapisz nazwę aktualnie zaznaczonego użytkownika
                    selectedDomainUser = domainUsername;
                }
            }
            catch (Exception ex)
            {
                // Obsługa ewentualnych błędów
                MessageBox.Show("Wystąpił błąd: " + ex.Message);
            }

            return domainUsers;
        }


What I have tried:

I have a working code just for local users and admin but for a domain users I didn't have any information to make it work.
Posted
Comments
Maciej Los 1-Dec-23 15:33pm    
Do you want to get Active Directory users? If so, then take a look here: Get List of Active Directory Users in C#[^] and here: Getting Users From Active Directory[^]

Note: you have to be sure that users are registered on Windows Server.

[Polish]:
Czy chcesz pobrać konta użytkowników ActiveDirectory?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900