Home > Active Directory, Application Integration > Get all active directory users using .NET

Get all active directory users using .NET

Few days back I had requirment for pulling all the active directory users and it into a list. I tried few nice codes from online help but they failed for pulling all the users from AD, also the users I expected from the LDAP query (Active Directory) were not right (seems my domain and ldap path wasn’t right at all).
So finally I did some R n D also taken help from Network Admin for right LDAP path and domain name ūüôā . After that everything started working well. Following is the code for same.

public DataTable GetActiveDirectoryUsers()
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† string ldapPath = ConfigurationManager.AppSettings[“LDAP”]; // e.g “LDAP://OU=ADSI,DC=ds,DC=microsoft,DC=com
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† string domainPath = ConfigurationManager.AppSettings[“DomainName”]; // not needed anymore¬†
                //Initiate directoryEntry object here by specifying the LDAP Path.
                DirectoryEntry directoryEntry = new DirectoryEntry(ldapName);
                // Create Directory Searcher object for firing search on the LDAP
                DirectorySearcher directorySearcher = new DirectorySearcher();
                // no need to define the datacolumns for the datatable here as it is typed datatable
                DataRow newUsersRow;
                {   // Set directorySearcher attributes here
                    directorySearcher.SearchRoot = directoryEntry;
                    directorySearcher.SearchScope = SearchScope.Subtree;
                    // Set The Fields/ColumnNames to be fetched
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“cn”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“name”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“givenName”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“sn”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“sAMAccountName”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“initials”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“l”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“co”);
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.PropertiesToLoad.Add(“mail”);
                    //define filter criteria for search here
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† directorySearcher.Filter = “(&(objectCategory=person)(objectClass=User)(givenname=*))”;
                    // Set the pagesize in order to get all records here
                    directorySearcher.PageSize = 1000;

                    // Fire the search query here and collect the results in SearchResultCollection object
                    SearchResultCollection resultCollection = directorySearcher.FindAll();

                            foreach (SearchResult result in resultCollection)
                                newUsersRow = activeDirectoryDT.NewUsersRow();
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.UserID = –currentUserID;

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† if (!((result.Properties[“sAMAccountName”][0]).ToString().ToUpper().StartsWith(domainName + “\\”)))
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.UserName = domainName + “\\” + (result.Properties[“sAMAccountName”][0]).ToString();
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.UserName = (result.Properties[“sAMAccountName”][0]).ToString();

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.AliasName = (result.Properties[“name”][0]).ToString();
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.Email = (result.Properties[“mail”][0]).ToString();
                                newUsersRow.groupid = 1;
                                newUsersRow.Active = false;
                                newUsersRow.CreatedBy = 0;
                                newUsersRow.CreatedDate = DateTime.Now;
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† newUsersRow.UserType = “EndUser”;
                                newUsersRow.UserTypeID = 3;

                        catch (Exception ex)
                            // do something here
                catch (Exception ex)
                  // do something here

            return activeDirectoryDT;               

In the above code few things are very important to note:

  1. LDAP path e.g “LDAP://OU=ADSI,DC=ds,DC=microsoft,DC=com” its very precise path for getting right DOMAIN
  2. Search Root and Search Scope sequnce must be as above, also the they must be set before listing the field name/column names that you want to pull out
  3. The Search Filter must be set after listing the field name/column names that you want to pull out
  4. The Page Size  must be set after search filter is set
  5. The Page Size Property helps you to pull out all of the Users. If this is not set then you will not get all users. This property is very similar to the Paging property of gridview which allows us to implement paging of the pulled data (here it does internal paging of the users and then returns the final composite users list)

I hope this will be helpful for you ūüôā

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: