Before moving on let's understand some assemblies and classes associated with Active Directory which are used to perform some operations related to Active Directory.
Assemblies
using System.DirectoryServices;
- This namespace provides easy access to Active directory.
- This namespace basically used to manipulate users,computers and security groups across the multiple directory services say Active Directory Domain Services(AD DS) and Active Directory Lightweight Directory Services(AD LDS) and also provide the access to users, computers and security groups.
Classes
PrincipalContext
- This class basically wrap server or domain, container and credentials against all operations.
DiectoryEntry
- This class is used to access the entries from schema entries, also let us fetch entries (attribute values) and update the same .
We have to use above assemblies for accessing and performing operation against Active Directory.
Add the following assemblies
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
Let's add the following class ClsUser with properties
- public class ClsUsers
- {
- public string AccountName { get; set; }
- public string DisplayName { get; set; }
- }
- private void GetADUsers()
- {
- dt.Columns.AddRange(new DataColumn[2]
- {
- new DataColumn("AccountName", typeof (string)),
- new DataColumn("GivenName", typeof (string))
- });
- using(var context = new PrincipalContext(ContextType.Domain, "enter your domain here"))
- {
- using(var searcher = new PrincipalSearcher(new UserPrincipal(context)))
- {
- foreach(var result in searcher.FindAll())
- {
- DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
- var clsuser = new ClsUsers();
- dt.Rows.Add(Convert.ToString(de.Properties["samAccountName"].Value), Convert.ToString(de.Properties["givenName"].Value));
- }
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- }
- }
Output

Closure
That's how we can fetch the Active Directory entries of user. Hope you find this helpful.
Happy coding!


Jorge MatarritaPosted Apr 24, 2020, 1:34 PM EDT
Excelente aporte!!!!
Dinesh GabhanePosted Nov 10, 2019, 8:18 PM EST
Thank you for sharing
Hadshana KamalanathanTOP 100Posted Jul 22, 2018, 2:39 AM EDT
Thanks for sharing
Viknaraj ManogararajahTOP 100Posted Jul 21, 2018, 9:44 PM EDT
Nice article, thank you for sharing
Shridhar SharmaPosted Aug 9, 2016, 9:22 AM EDT
Thanks friends :)
Asfend YarPosted Feb 29, 2016, 8:38 AM EST
good
Sonu ChaudharyPosted Feb 25, 2016, 1:37 AM EST
good one!
RubolPosted Feb 9, 2016, 7:03 PM EST
Nice
Shridhar SharmaPosted Jan 4, 2016, 1:17 AM EST
Thank you Santhakumar Munuswamy, Humayun Kabir Mamun and P K Yadav :)
Praveen KumarPosted Jan 2, 2016, 8:57 PM EST
Very Nice.. Sridhar
Humayun Kabir MamunPosted Jan 2, 2016, 7:15 PM EST
Nice...
Santhakumar MunuswamyPosted Jan 2, 2016, 5:19 PM EST
Thanks for nice share