By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

getting all users in a windows domain

P: 22
Ok currently i am working in an environment with 20 computers all in the domain, which has about 40 users. These 40 users change often. I need to write an application that pulls all the usernames and if possible more information(i.e. There first and last name etc...) from the domain server. I am an administrator so i have full access to this server and can get to all the information manually through active directory on Windows Server 2003, but i found it redundant to have to type the information twice(i.e. once in active directory and again in the system i am tweaking). I have been researching the new classes for domains in the .net framework 2 and have come up blank any help would be great.

Apr 25 '07 #1
Share this Question
Share on Google+
4 Replies

P: 22
I have started finding some things about this.

i have found the code to get the current user on the domain.

Expand|Select|Wrap|Line Numbers
  1. my.User.Name
i still need help on getting a collection of all of the users on the domain
Apr 26 '07 #2

Expert 100+
P: 759
For this you have to query the active directory.

The Assembly is System.Directory Services.

Here Is for you,

//namespace required
using System.DirectoryServices;

//creating an instance of DirectoryEntry pointing to the //“TestOU” with valid credentials.
DirectoryEntry DE = new DirectoryEntry(@"LDAP://Norchio/OU=TestOU,DC=norchio,DC=com","Username","password" ,AuthenticationTypes.ServerBind | AuthenticationTypes.Secure);

// creating an instance of DirectorySearcher which will search in AD.
System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(DE);

//setting up the search filters like if we want users from OU in AD
mySearcher.Filter = ("(objectClass=user)");
Console.WriteLine("Users Listing from Active Directory");

foreach(System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
ResultPropertyCollection RCol = resEnt.Properties;
foreach(string myKey in RCol.PropertyNames)
string tab = " ";
if (myKey == "samaccountname")
Console.WriteLine(myKey + " ");
foreach(object myCollection in RCol[myKey])
Console.WriteLine(tab + myCollection);
Apr 26 '07 #3

P: 22
I am very greatful for your help, this is much appreciated... although there is something that i am not understanding. I Primarily program in 2005 so this code is somewhat foreign. I can interpret most of the code but can you explaing this line?

Expand|Select|Wrap|Line Numbers
  1. //creating an instance of DirectoryEntry pointing to the //“TestOU” with valid credentials.
  2. DirectoryEntry DE = new DirectoryEntry(@"LDAP://Norchio/OU=TestOU,DC=norchio,DC=com","Username","password",AuthenticationTypes.ServerBind | AuthenticationTypes.Secure);
i do not understand what the first 3 parameters after the @

what path am i supposed to enter?
Apr 27 '07 #4

P: 22
bump. bump. bump.
May 3 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.