how to list out users from pc either he created in winnt folder(local users and groups) or active directory( under domain) using c#.net

▼魔方 西西 提交于 2019-12-25 03:55:26

问题


I have implemented a c# windows application.

For that application I want to provide login from windows user credentails.

Now the issue is I need to get the users list from windows pc through code.

How to get users list eaither user created in winnt folder (under local users and groups), or user created in active directory domain.

I have an idea for get users list from winnt folder that is

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
// DirectoryEntry admGroup = localMachine.Children.Find("Guests", "group");
// DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
DirectoryEntry admGroup = localMachine.Children.Find("users", "group");
//DirectoryEntry admGroup = localMachine.Children.Find("TestUser1", "group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
    DirectoryEntry member = new DirectoryEntry(groupMember);
    listBox1.Items.Add(member.Name);
} 

But now I want to list out users, if he is exist in active directory or winnt folder.

Any body give me code for this to cross verify users from both throug c# code


回答1:


For the Active Directory part, and if you're using .NET 3.5 or newer, you can look into the new System.DirectoryServices.AccountManagement namespace.

You can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
UserPrincipal qbeUser = new UserPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

List<string> userNames = new List<string>();

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal"
    if(!userNames.Contains(found.Name))
    {
       userNames.Add(found.Name);
    }
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

For the local machine accounts, you can do basically the same thing - just with a different PrincipalContext:

// create your local machine context
PrincipalContext local = new PrincipalContext(ContextType.Machine);

The rest of the code is identical - but your principal objects might have a lot fewer of the properties actually populated (since the local machine accounts don't have as much information stored for them as the Active Directory accounts) - but every principal definitely does have at least a .Name property!



来源:https://stackoverflow.com/questions/8455100/how-to-list-out-users-from-pc-either-he-created-in-winnt-folderlocal-users-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!