Get a list of users in a domain

Posted on 2004-11-30
Last Modified: 2012-06-27

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.
Question by:cherieannc
    LVL 11

    Expert Comment

    You can do it with WMI.

    Author Comment

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

    Accepted Solution

    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:
    LVL 2

    Expert Comment


    or you can use the Win32API

    using System.Runtime.InteropServices;


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

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


    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




    Author Comment


    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?


    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 :-)

    LVL 11

    Expert Comment

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

    Expert Comment

    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

    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.

    Author Comment

    Thanks guys :-) appreciate the suggestions....

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    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…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now