locked
How to get list users in a group of Active Directory RRS feed

  • Question

  • User-576945844 posted

    i have to get list of users in a group of  Active directory i have different group when user select the group name itshould fill the list of users belog to that group in gridview

     i am using below code for getting the list of user but its not getting all the users in Active Directory

     try
            {
                
                string domainAndUsername = "domain" + @"\" + "admin";
               
                DirectoryEntry myLdapConnection = new DirectoryEntry(LdapPath, domainAndUsername, passwrd);
                DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };
            
                search.CacheResults = true;
                SearchResultCollection allResults = search.FindAll();
                DataTable resultsTable = new DataTable();
                resultsTable.Columns.Add("UserID");
                resultsTable.Columns.Add("EmailID");
                foreach (SearchResult searchResult in allResults)
                {
    
                   
    
                    MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];
                
                    if (myUser == null)
                    {
                        DataRow dr = resultsTable.NewRow();
                        dr["UserID"] = searchResult.Properties["SAMAccountName"][0].ToString();
                        if (searchResult.Properties["mail"].Count > 0)
                        {
                            dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                        }
                        else
                        {
                            dr["EmailID"] = "";
                        }
                        resultsTable.Rows.Add(dr);
                    }
                    else
                    { }
                }
                grdViewAllADSUsers.DataSource = resultsTable;
                grdViewAllADSUsers.DataBind();
              
     
            }
            catch (Exception)
            {
            }

    Sunday, February 9, 2014 3:25 AM

Answers

  • User1508394307 posted

    Try to add page size http://msdn.microsoft.com/en-us/library/ms180880(v=vs.80).aspx 

    search.PageSize = 10;
    SearchResultCollection allResults = search.FindAll();

    and see if it will make any difference.

    In general, if you need to get users from particular group you need to add that group in filter. 

    So, instead of "(objectClass=user)" you would need to have

    "(&(objectClass=user)(memberOf=CN=YourGroup,OU=Users,DC=YourDomain,DC=com))"

    where "YourGroup,OU=Users,DC=YourDomain,DC=com" is a path to your group. 

    To find the path you can use ADSI or some LDAP tools like LDAP Browser by Softerra.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 9, 2014 8:03 AM

All replies

  • User1508394307 posted

    its not getting all the users

    How much users does it return?

    Remove try..catch to see if there is any error.

    Sunday, February 9, 2014 4:08 AM
  • User-576945844 posted

    i try after removing there is no error ,, 

    i dont know how much  when i try to search , i am getting some users in the list and some are missing , may be 50% of users are missing . 

    Sunday, February 9, 2014 4:16 AM
  • User1508394307 posted

    Try to add page size http://msdn.microsoft.com/en-us/library/ms180880(v=vs.80).aspx 

    search.PageSize = 10;
    SearchResultCollection allResults = search.FindAll();

    and see if it will make any difference.

    In general, if you need to get users from particular group you need to add that group in filter. 

    So, instead of "(objectClass=user)" you would need to have

    "(&(objectClass=user)(memberOf=CN=YourGroup,OU=Users,DC=YourDomain,DC=com))"

    where "YourGroup,OU=Users,DC=YourDomain,DC=com" is a path to your group. 

    To find the path you can use ADSI or some LDAP tools like LDAP Browser by Softerra.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 9, 2014 8:03 AM