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();
        } 
    }
}

7 comments:

Featured Post

Build a Secure MCP Server on Azure Functions for Copilot Studio Agents (Entra ID OAuth)

Model Context Protocol (MCP) is quickly becoming the standard way to give AI agents real tools. Copilot Studio can call any remote MCP serve...

Popular posts