?
Solved

Get a list of users in a domain

Posted on 2004-11-30
8
Medium Priority
?
519 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:cherieannc
8 Comments
 
LVL 11

Expert Comment

by:Buffon
ID: 12713652
You can do it with WMI.
0
 

Author Comment

by:cherieannc
ID: 12713681
Thanks Buffon but uhmm... how? and what is WMI? sorry but I'm really new to all these things... thanks...
0
 
LVL 11

Accepted Solution

by:
Buffon earned 1000 total points
ID: 12713847
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:Tdl
ID: 12735669
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
 

Author Comment

by:cherieannc
ID: 12750962
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
 
LVL 11

Expert Comment

by:Buffon
ID: 12751861
You can use 'where' in 'select' clause or just run through the users you get and check if wanted user is among them.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12751875
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
 

Author Comment

by:cherieannc
ID: 12761909
Thanks guys :-) appreciate the suggestions....
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question