Get a list of users in a domain

Hello!

I am developing an application that needs to get a list of users in a particular domain on the network. Does anyone have any idea how I should go about doing this?

I am aware of the System.DirectoryServices namespace in C# but I am not sure if that only allows for a single user. Multiple users on multiple computers must be able to see the list of users in the domain.

Basically this is what the application should do:

1. Get a list of users in the domain on the network.
2. Add this list of users into a table in SQL Server.

The whole process involves adding all users in the domain into a table in SQL server so that when the users log on to the network using their windows authenticated credentials they don't have to supply any other username or password to access the database. The application will just look up their current credentials with the ones listed in the SQL Server table, if it finds a match it will allow the user to access the database. So, How am I going to get this list of users in the domain and how will I also be able to get the windows authenticated credentials of the current user accessing the database?

I hope I made my question clear, I'm fairly new to c# and SQL Server. Thanks.
cherieanncAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BuffonCommented:
You can do it with WMI.
0
cherieanncAuthor Commented:
Thanks Buffon but uhmm... how? and what is WMI? sorry but I'm really new to all these things... thanks...
0
BuffonCommented:
ok, here you go, its just some simple code, but I think it will do for the beginning:

using System.Management;

ObjectQuery objectQuery = new ObjectQuery("select * from Win32_UserInDomain");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(objectQuery);
foreach (ManagementObject share in searcher.Get())
{
    foreach (PropertyData diskProperty in share.Properties)
    {
       Console.WriteLine("Property = " + diskProperty.Name + " :: " + diskProperty.Value);
    }
}

also check this link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/monitordotnet.asp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

TdlCommented:
Hi,

or you can use the Win32API

using System.Runtime.InteropServices;

...

            [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
                  public struct USER_INFO_0
            {
                  public String Username;
            }



            [DllImport("Netapi32.dll")]
            extern static int NetUserEnum([MarshalAs(UnmanagedType.LPWStr)]
                  string servername,
                  int level,
                  int filter,
                  out IntPtr bufptr,
                  int prefmaxlen,
                  out int entriesread,
                  out int totalentries,
                  out int resume_handle);


private void button4_Click(object sender, System.EventArgs e)
            {
                  ArrayList users = new ArrayList();
                  int EntriesRead;
                  int TotalEntries;
                  int Resume;
                  IntPtr bufPtr;

                  NetUserEnum("",0,2,out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
                  if(EntriesRead > 0)
                  {
                        USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
                        IntPtr iter = bufPtr;
                        for(int i=0; i<EntriesRead;i++)
                        {
                              Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter, typeof(USER_INFO_0));
                              iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));

                              users.Add(Users[i].Username);
                        }      
                  }
            
            }


This would enum the Users in the current domain ... you can specify a specifiy domainctroller or a domain. And this example only gives you the username ... with an other level you could get more information. have a look at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netuserenum.asp

--
HTH,

Marc

0
cherieanncAuthor Commented:
Buffon:

Thanks for the tip, I'm still trying to figure out how everything works. I need only the usernames of each person that is part of the domain. Is there anyway I can check if the user is a member of the domain?

Marc:

Thanks but we are required to use only managed code for our application. So I don't think I would be able to use the code you posted. But thank you anyway :-)

Cherie
0
BuffonCommented:
You can use 'where' in 'select' clause or just run through the users you get and check if wanted user is among them.
0
ihenryCommented:
When you want to authenticate a user account against its password, or your app may run on machine which is not under the same AD domain or you want to perform user management operation, then you already have some compelling reasons to use DirectoryServices instead of the other ways. And when dealing with user account it performs the same thing with NetUserEnum native API under the hood, but in "managed" way.

The following platform sdk articles should give you some ideas on DirectoryServices API
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/portal.asp

However, for some other reasons it might be better to use built-in windows authentication to authenticate a user account. For ASP.NET you can rely on IIS and Windows authentication, for other application you can utilize LogonUser Windows API. And since you're using SQL Server database, you have one more compelling reason to link your SQL Server database to Active Directory and use OPENQUERY to list all AD user accounts instead of creating redundancy data. Obviously, the latter option should be the most easiest.
0
cherieanncAuthor Commented:
Thanks guys :-) appreciate the suggestions....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.