In this article am going to show how we can get all
active directory users in asp.net c#.
Below is the aspx code, here after getting all users I
am binding their Name and email in a asp.net 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>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table cellpadding="4" cellspacing="4" width="90%" align="center" style="border: 1px solid
Green;">
<tr>
<td>
<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" />
</asp:GridView>
</td>
</tr>
</table>
</asp:Content>
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)
GetAllADUsers();
}
public void GetAllADUsers()
{
try
{
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();
resultsTable.Columns.Add("UserID");
resultsTable.Columns.Add("EmailID");
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();
}
else
{
dr["EmailID"] = "";
}
resultsTable.Rows.Add(dr);
}
else
{ }
}
grdViewAllADSUsers.DataSource = resultsTable;
grdViewAllADSUsers.DataBind();
}
catch (Exception)
{
}
}
protected void grdViewAllADSUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdViewAllADSUsers.PageIndex = e.NewPageIndex;
GetAllADUsers();
}
}
My Output:

Image 1.