views:

51

answers:

2

Hi there!

I have a ASP.NET Website project and I need to list all the users and their groups on my Windows system. I have set the identity impersonation to true and provided the username and password of the admin in the web.config. Where do I start?

Thanks in advance.

Update:

I have the following code at the moment -

    var machine = new DirectoryEntry("WinNT://<IP ADDRESS>");
                foreach (DirectoryEntry child in machine.Children)
                {
                   // get the child's group(s).
                }

When I debug, I can see the list of users in machine.Children. How do I find the group(s) that this user belongs to?

A: 

This article covers how to talk to Active Directory and should get you where you want to go: http://www.codeproject.com/KB/system/everythingInAD.aspx

To get users, you would do something like this:

public List<string> GetUserList()
{
        string DomainName="";
        string ADUsername="";
        string ADPassword="";

        List<string> list=new List<string>();
        DirectoryEntry entry=new DirectoryEntry(LDAPConnectionString, ADUsername, ADPassword);
        DirectorySearcher dSearch=new DirectorySearcher(entry);
        dSearch.Filter="(&(objectClass=user))";

        foreach(SearchResult sResultSet in dSearch.FindAll())
        {
            string str=GetProperty(sResultSet, "userPrincipalName");
            if(str!="")
                list.Add(str);
        }
        return list;
}
o6tech
Thanks, so it looks like I need to setup LDAP server on the Windows machine. Is there a way, I can get the list without setting up LDAP?
A: 

You probably want to start with the DirectoryEntry and Active Directory support in .net.

Here's a good resource: http://www.codeproject.com/KB/system/everythingInAD.aspx

Local access is similar, even if you're not in a domain:

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" +
               Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators",
               "group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members) {
  DirectoryEntry member = new DirectoryEntry(groupMember);
  //...
}
Philip Rieck