Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Get List of Active Directory Users in ASP.NET Csharp

Viewed:  39483 
Posted On:  25/08/2013 21:14:38 
How to get all AD users in c#. How we can show their name and email in a Grid...? 

In this article am going to show how we can get all active directory users in c#. 
Below is the aspx code, here after getting all users I am binding their Name and email in a Grid.

<%@ Page Title="AD USERS" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">


<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <table cellpadding="4" cellspacing="4" width="90%" align="center" style="border: 1px solid Green;">



                <asp:GridView ID="grdViewAllADSUsers" runat="server" AllowPaging="True" CellPadding="4"

                    Width="98%" ForeColor="#333333" GridLines="None" OnPageIndexChanging="grdViewAllADSUsers_PageIndexChanging">

                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

                    <EditRowStyle BackColor="#999999" />

                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

                    <SortedAscendingCellStyle BackColor="#E9E7E2" />

                    <SortedAscendingHeaderStyle BackColor="#506C8C" />

                    <SortedDescendingCellStyle BackColor="#FFFDF8" />

                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />








My aspx.cs code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.DirectoryServices;

using System.Web.Security;


public partial class _Default : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)


        if (!Page.IsPostBack)




    public void GetAllADUsers()





            DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://YouDomainName");

            DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };

            search.CacheResults = true;

            SearchResultCollection allResults = search.FindAll();

            DataTable resultsTable = new DataTable();



            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();




                        dr["EmailID"] = "";





                { }


            grdViewAllADSUsers.DataSource = resultsTable;




        catch (Exception)




    protected void grdViewAllADSUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)


        grdViewAllADSUsers.PageIndex = e.NewPageIndex;




My Output:

AD Users.png

Image 1.

By Hammad  On  25/05/2016 21:52:25
I want all column of all users from active directory with employee number
By Jorge  On  12/11/2014 10:49:46
could some one help me with getting AD users
By Peggy  On  30/08/2014 15:38:50
So glad I found your site! Had been looking all over on how to output AD list to a grid. So now I have a question. I would like to only output AD item when employeeID > " "? I'm finding too many non-user lines in our AD. Thanks ahead!
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us