Click here to Skip to main content
15,884,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

in web application i need to pull out all the users from active directory, can some one help me out on this, i googled and got so many code, none is working fine, can some one provide the working code for this? early response would be appreciated.
Posted

 
Share this answer
 
Try this:
Active Directory login[^]

Here in this link I explained everything. This will help you a lot in case of working directory.



--Amit
 
Share this answer
 
C#
using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainToBeSearched))
{
    using (DirectorySearcher searcher = new DirectorySearcher())
    {
        //Set root for searching
        searcher.SearchRoot = domain;
        //Set filters for searching user
        searcher.Filter = FormFilter("user", userName + "*");
        searcher.SearchScope = SearchScope.Subtree;
        searcher.PageSize = 10;
        //Get all registered user on basis of passed filter
        using (SearchResultCollection adResult = searcher.FindAll())
        {
            DataRow dtrUsers = null;
            foreach (SearchResult result in adResult)
            {
                if (dtUsers.Select("key='" + result.Properties["samaccountname"][0].ToString() + "'").Length <= 0)
                {
                    //Add user to datatable
                    dtrUsers = dtUsers.NewRow();
                    dtrUsers[0] = result.Properties["samaccountname"][0].ToString();
                    dtrUsers[1] = result.Properties["DisplayName"][0];
                    dtUsers.Rows.Add(dtrUsers);
                }

            }
        }
    }
}



hope this should work, correct me if any issues!
 
Share this answer
 

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