This post will explain how to get information of all users from Active Directory. Also this post will demonstrate the use of recursion.
//datatable to store users information
DataTable objDTAD = new DataTable("export");
objDTAD.Columns.Add(new DataColumn("Logon Name"));
objDTAD.Columns.Add(new DataColumn("Name"));
objDTAD.Columns.Add(new DataColumn("Company"));
objDTAD.Columns.Add(new DataColumn("Email Address"));
objDTAD.Columns.Add(new DataColumn("Account Enabled"));
private void GetUserInfo(string path, string ou)
{
//path is your LDAP path of AD
DirectoryEntry de = new DirectoryEntry(path);
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName == "organizationalUnit")
{
//calling the same method if a organizationalUnit found under the path provided
GetUserInfo(child.Path, child.Name);
}
else if (child.SchemaClassName == "user")
{
DataRow objDRAD = objDTAD.NewRow();
objDTAD.Rows.Add(objDRAD);
objDRAD[0] = ValidateProperty(child.Properties["SAMAccountName"]);
objDRAD[1] = ValidateProperty(child.Properties["cn"]);
objDRAD[2] = ou.Replace("OU=", "");
objDRAD[3] = ValidateProperty(child.Properties["mail"]);
int userAccountControl = Convert.ToInt32(child.Properties["userAccountControl"][0]);
if ((userAccountControl & 2) > 0)
{
objDRAD[4] = "Disabled";
}
else
{
objDRAD[4] = "Enabled";
}
}
}
}
//to validate users account property is having value.
private string ValidateProperty(PropertyValueCollection coll)
{
if (coll.Count == 0)
return "";
else
{
return coll[0].ToString();
}
}
Python training in bangalore
ReplyDeleteMachine learning course in bangalore
Data Science Online Course in Bangalore
Learn Storage Automation (Using Python) in Bangalore BTM
Cloud Computing Training in Bangalore BTM