AddyProg
AddyProg

Reputation: 3050

How can I get list of users of a specific domain?

Is there any way that i can get number of users on a network by domain name ? For example if there are 50 users connected to a network using windows. Can I get the list of all 50 users(User Names) ?

Upvotes: 2

Views: 277

Answers (1)

user2440074
user2440074

Reputation:

You can get list of all users like this:

        static List<string> GetUsersInForest()
        {
            var winIdentity = System.Web.HttpContext.Current.User.Identity as System.Security.Principal.WindowsIdentity;
            if (winIdentity == null) throw new InvalidOperationException();

            using (var ictx = winIdentity.Impersonate())
            {
                List<string> users = new List<string>();
                using (var f = System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest())
                {
                    foreach (Domain d in f.Domains)
                    {
                        using (d)
                        {
                            foreach (string user in GetUsersInDomain(d.Name))
                            {
                                int idx = users.BinarySearch(user);
                                if (idx < 0) users.Insert(~idx, user);
                            }
                        }
                    }
                }

                return users;
            }
        }

And then filter users as you want

Upvotes: 2

Related Questions