Tuesday, July 5, 2011

Get the User Names from Active Directory using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.AccountManagement;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry domain = new DirectoryEntry("LDAP://domainname.com/CN=Users,DC=domainname,DC=com");

            foreach (DirectoryEntry child in domain.Children)
            {
                Console.WriteLine(child.Name);
            }
            Console.WriteLine("----------------------------------------------------------");

            DomainCollection domains = Forest.GetCurrentForest().Domains;
            foreach (Domain domain1 in domains)
            {
                Console.WriteLine(domain1.Name);
            }
            Console.WriteLine("----------------------------------------------------------");
            DirectoryEntry domain2 = new DirectoryEntry("LDAP://domainname.com/CN=Computers,DC=domainname,DC=com");
            foreach (DirectoryEntry child1 in domain2.Children)
            {
                Console.WriteLine(child1.Name);
            }
            Console.ReadKey();
        } 
    }
}

5 comments:

Featured Post

Develop Azure AI services applications securely by using Azure Key Vault and Run Text Analytics (C#)

//Develop Azure AI services applications securely by using Azure Key Vault and Run Text Analytics (C#) //Create an Azure AI services account...

Popular posts