How to list Windows users and groups in ASP.NET?

后端 未结 2 565
我在风中等你
我在风中等你 2020-12-11 08:32

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 usern

相关标签:
2条回答
  • 2020-12-11 08:35

    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);
      //...
    }
    
    0 讨论(0)
  • 2020-12-11 08:59

    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;
    }
    
    0 讨论(0)
提交回复
热议问题