Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Friday, 11 March 2016

Get all users from active directory using c#


I needed to get all members  from active directory using c#.
Now the solution I have found is as follows


using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();
the solution I have provided is from http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory


Now the directory entry contains properties defined as string or object and you need to make sure that the data are accessed correctly.



Wednesday, 9 March 2016

C# Querying Organisational Units in Active directory

I am working on active directory queries. I have decided to share come code I have found, coded and updated.

Reference :
AD: Active Directory
OU: OrganisationalUnit (used to structure your AD)

How to get Organisational units from AD using c# & LDAP.

NOTE The important information here is:
Each structure in active directory have its own name and many times I have come across of misspelling the types. For OU we have to setup filter to search only on:

objectCategory = organizationalUnit


Now the full code is as follows


// connect to "RootDSE" to find default naming context
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");

string defaultContext = rootDSE.Properties["defaultNamingContext"][0].ToString();

// bind to default naming context - if you *know* where you want to bind to - 
// you can just use that information right away
DirectoryEntry domainRoot = new DirectoryEntry("LDAP://" + defaultContext);

// set up directory searcher based on default naming context entry
DirectorySearcher ouSearcher = new DirectorySearcher(domainRoot);

// SearchScope: OneLevel = only immediate subordinates (top-level OUs); 
// subtree = all OU's in the whole domain (can take **LONG** time!)
ouSearcher.SearchScope = SearchScope.OneLevel;
// ouSearcher.SearchScope = SearchScope.Subtree;

// define properties to load - here I just get the "OU" attribute, the name of the OU
ouSearcher.PropertiesToLoad.Add("ou");

// define filter - only select organizational units
ouSearcher.Filter = "(objectCategory=organizationalUnit)";

// do search and iterate over results
foreach (SearchResult deResult in ouSearcher.FindAll())
{
    string ouName = deResult.Properties["ou"][0].ToString();
}
Links to Stack Overflow

http://stackoverflow.com/questions/16810382/getting-all-ous-from-a-active-directory