Library that provides a helper class to work with Active Directory.
$ dotnet add package ZidUtilities.CommonCode.DataAccess.ActiveDirectoryActive Directory management and query utilities for enterprise directory services.
AdManager: Main Active Directory manager
AdManagerFilter: Advanced filtering for directory searches
AdManagerPath: Active Directory path management
BasicEmployee: Employee data model
AdAttribute: Active Directory attribute wrapper
AdKeyAttributes: Key attribute definitions
.NET Framework 4.8
Add a reference to CommonCode.DataAccess.ActiveDirectory.dll in your project and ensure you have the required System.DirectoryServices references.
using ZidUtilities.CommonCode.DataAccess.ActiveDirectory;
// Initialize AD Manager with domain credentials
var adManager = new AdManager(
domain: "CONTOSO",
username: "administrator",
password: "P@ssw0rd"
);
// Or connect to specific domain controller
var adManager = new AdManager(
ldapPath: "LDAP://DC01.contoso.com",
username: "administrator",
password: "P@ssw0rd"
);
// Search for a user by username
AdManager activeDirectoryManager = new AdManager("contoso.com");
activeDirectoryManager.Paths.Add(new AdManagerPath("Worker", "Users"));
BasicEmployee user = activeDirectoryManager.GetBasicEmployee("jdoe");
if (user != null)
{
Console.WriteLine($"Found user: {user.DisplayName}");
Console.WriteLine($"Email: {user.Email}");
Console.WriteLine($"Department: {user.Department}");
}
// Search for users by filter
activeDirectoryManager.Filter = AdManagerFilter.CreateFilter().FilterBy(FilterableAttribute.UserName, FilterComparer.Equals, "jane.doe");
activeDirectoryManager.Attributes.Add(new AdAttribute("sAMAccountName", "UserName", AdType.String));
activeDirectoryManager.Attributes.Add(new AdAttribute("employeeNumber", "EmployeeId", AdType.String));
activeDirectoryManager.Attributes.Add(new AdAttribute("distinguishedName", "DistinguishedName", AdType.String));
DataTable results = activeDirectoryManager.QueryActiveDirectory();
if (results != null && results.Rows.Count > 0)
{
string employeeId = results.Rows[0]["EmployeeId"].ToString();
string userName = results.Rows[0]["UserName"].ToString();
string distinguishedName = results.Rows[0]["DistinguishedName"].ToString();
Console.WriteLine($"Employee ID: {employeeId}, User Name: {userName}, DN: {distinguishedName}");
}
// Update user properties
AdManager activeDirectoryManager = new AdManager("contoso.com");
activeDirectoryManager.Paths.Add(new AdManagerPath("Worker", "Users"));
// Update single attributes
activeDirectoryManager.UpdateUserProperty("telephoneNumber", "555-1234", "CN=Jane Doe,OU=IT,OU=Users,DC=faradayfuture,DC=com");
// Another way to update single attribute
string username = "jdoe";
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://DC=contoso,DC=com");
DirectoryEntry user;
using (DirectorySearcher searcher = new DirectorySearcher(searchRoot))
{
searcher.Filter = $"(&(objectClass=user)(sAMAccountName={username}))";
SearchResult result = searcher.FindOne();
if (result != null)
{
user = result.GetDirectoryEntry();
activeDirectoryManager.UpdateUserProperty("title", "Sr. Application Developer", user);
}
else
{
Console.WriteLine("User not found.");
}
}
// Update multiple attributes at once
List<AdAttribute> attributesToUpdate = new List<AdAttribute>
{
new AdAttribute("mobile", "555-9999", AdType.String),
new AdAttribute("manager", "CN=Jane Smith,OU=Users,DC=contoso,DC=com", AdType.String),
new AdAttribute("description", "Full stack developer", AdType.String)
};>
activeDirectoryManager.AttributeBatchUpdate("CN=Jane Doe,OU=IT,OU=Users,DC=faradayfuture,DC=com", attributesToupdate);
//OR
//activeDirectoryManager.AttributeBatchUpdate(<DirectoryEntry user>, attributesToupdate);
// Authenticate user credentials
AdManager adManager = new AdManager("contoso.com");
adManager.Paths.Add(new AdManagerPath("Worker", "Users"));
bool isValid = adManager.IsValidCredential("jdoe", "UserPassword123");
if (isValid)
{
Console.WriteLine("Authentication successful!");
}
else
{
Console.WriteLine("Authentication failed!");
}
// Build LDAP paths safely
var path = new AdManagerPath("DC=contoso,DC=com");
// Add organizational units
path.AddOU("IT");
path.AddOU("Users");
// Get full LDAP path
string ldapPath = path.ToString();
// Result: "LDAP://OU=Users,OU=IT,DC=contoso,DC=com"
// Parse existing path
var existingPath = AdManagerPath.Parse("LDAP://CN=John Doe,OU=Users,DC=contoso,DC=com");
string cn = existingPath.GetCommonName(); // "John Doe"
using System;
try
{
var adManager = new AdManager("CONTOSO", "admin", "password");
var user = adManager.FindUser("jdoe");
if (user == null)
{
Console.WriteLine("User not found");
}
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("Insufficient permissions: " + ex.Message);
}
catch (DirectoryServicesCOMException ex)
{
Console.WriteLine("AD error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Unexpected error: " + ex.Message);
}