Wednesday, September 4, 2013

Get the List of Active Directory Users in C#

The below code will fetch the list of users form active directory.

Code Download

1)  Include the namespace System.DirectoryServices in the page.
  
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Windows.Forms;



The System.DirectoryServices namespace provides easy access to Active Directory Domain Services from managed code. The namespace contains two component classes, DirectoryEntry and DirectorySearcher, which use the Active Directory Services Interfaces (ADSI) technology.


2) Below code will fetch the uses from given domain name

  public class AllUsers
    {
        public List<string> GetUser(string domainName)
        {
            return DirectoryIdentity("LDAP://" + domainName);
        }
        private List<string> DirectoryIdentity(string path)
        {
            DirectoryEntry directoryEntry1 = new DirectoryEntry(path);
            try
            {
                //ArrayList allUsers = new ArrayList();
                List<string> allUsers = new List<string>();
                DirectorySearcher AllUsers = new DirectorySearcher(directoryEntry1);
                AllUsers.PropertiesToLoad.Add("sn");  // surname = last name
                AllUsers.PropertiesToLoad.Add("givenName");  // given (or first) name
                AllUsers.PropertiesToLoad.Add("mail");  // e-mail addresse
                AllUsers.PropertiesToLoad.Add("userPrincipalName");  // user logon name
                AllUsers.PropertiesToLoad.Add("cn");
                AllUsers.SearchScope = SearchScope.Subtree;
                AllUsers.Filter = "(&(objectClass=user)(objectCategory=person))";
                AllUsers.PropertiesToLoad.Add("samaccountname");
                int count = AllUsers.FindAll().Count;
                SearchResultCollection results = AllUsers.FindAll();
                SearchResult result;
                if (results != null)
                {
                    for (int counter = 0; counter < results.Count; counter++)
                    {
                        result = results[counter];
                        if (result.Properties.Contains("samaccountname"))
                        {
                            allUsers.Add((String)result.Properties["samaccountname"][0]);
                        }
                    }
                }
                allUsers = allUsers.OrderBy(x => x).ToList();
                return allUsers;
            }
            catch
            {
                throw;
            }
        }
    }



3) You can pass the domain name as below code in the windos applicaion

 private void btnClick_Click(object sender, System.EventArgs e)
        {
            string domainName = txtDomianName.Text.Trim();
            if (string.IsNullOrEmpty(domainName))
            {
                MessageBox.Show("Domain name can not be blank.");
                return;
            }
            AllUsers allUser = new AllUsers();
            List<string> userList = allUser.GetUser(domainName);
            cbUser.DataSource = userList;
        } 


1 comment:

  1. Hello there! Do you use Twitter? I'd like to follow you if that would be ok.

    I'm undoubtedly enjoying your blog aand look forward to new posts.


    Also visit mmy web page ... wheel turning lathe ()

    ReplyDelete