logo

,

Search User in Active Directory C#

In this article we will discuss how we can search specific user, and user details in Active Directory using C#.

Getting Started

I will be using a windows forms application for this example. The form consists of Textbox, Button and a Data Grid View. The user will enter the required User Id in the textbox, on button click our application will connect with Active Directory, and display user details in the DataGridView.

Screen

 

Step 1 – Add System.DirectoryServices NuGet Package

Nuget Package

 

Step 2 – Connect to Active Directory and Get User


        private void btnSearch_Click(object sender, EventArgs e)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://YourDomain.com");
            DirectorySearcher ds = new DirectorySearcher(de);
            ds.Filter = "(SAMAccountName=" + txtSearch.Text + ")";
            SearchResult result = ds.FindOne();
            
        }

Notes:

  1. Mostly we need to connect to Active Directory using the service account credentials. In that case we just need to modify the first line of the code, by additionally passing the service account credentials as parameters.
    
    DirectoryEntry de = new DirectoryEntry("LDAP://YourDomain.com", "ServiceAccountUsername", "ServiceAccountPassword");
    
    
  2. In the above code snippet we have connected to the Active Directory and searched the user using the “SAMAccountName” attribute. The SAMAccountName attribute is the logon name. In this example we are searching for specific user using the “SAMAccountName ” attribute, you can search using any other attribute as well. For example search user by email address.
    ds.Filter = "(mail=" + txtSearch.Text + ")";
    
  3. The searched user details are available in the SearchResult variable “result”

 

Step 3 – Display Result in Data Grid View

Now, we will read the SearchResult variable and add the results to our DataTable. This DataTable will bind to the DataGridView for displaying the result.

Our final complete code looks as below:


        private void btnSearch_Click(object sender, EventArgs e)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://YourDomain.com", "ServiceAccountUsername", "ServiceAccountPassword");
            DirectorySearcher ds = new DirectorySearcher(de);
            ds.Filter = "(SAMAccountName=" + txtSearch.Text + ")";
            SearchResult result = ds.FindOne();

            if (result != null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("EmailID");
                dt.Columns.Add("Telephone Extension");
                dt.Columns.Add("Department");
                dt.Columns.Add("Job Title");
                DataRow dr = dt.NewRow();

                DirectoryEntry entry = result.GetDirectoryEntry();

                if (entry.Properties["cn"].Count > 0)
                {
                    dr["Name"] = entry.Properties["cn"][0].ToString();
                }
                else
                    dr["Name"] = "";

                if (entry.Properties["mail"].Count > 0)
                {
                    dr["EmailID"] = entry.Properties["mail"][0].ToString();
                }
                else
                    dr["EmailID"] = "";

                if (entry.Properties["telephoneNumber"].Count > 0)
                {
                    dr["Telephone Extension"] = entry.Properties["telephoneNumber"][0].ToString();
                }
                else
                    dr["Telephone Extension"] = "";

                if (entry.Properties["department"].Count > 0)
                {
                    dr["Department"] = entry.Properties["department"][0].ToString();
                }
                else
                    dr["Department"] = "";

                if (entry.Properties["title"].Count > 0)
                {
                    dr["Job Title"] = entry.Properties["title"][0].ToString();
                }
                else
                    dr["Job Title"] = "";

                dt.Rows.Add(dr);

                dataGridView1.DataSource = dt;
            }
        }

Step 4 – Demo

 

Wrapping Up

I hope, this blog post has helped you in learning and understanding how to search a user in Active Directory using C#.

Don’t forget to check out: Generate QR Code in Asp.net Core with 4 Simple Steps

Thank You.

Leave a Reply

Your email address will not be published.

Share on facebook
Share on twitter
Share on linkedin

Related articles