简体   繁体   English

如何使用c#.net从他在winnt文件夹(本地用户和组)或活动目录(域下)中创建的pc中列出用户

[英]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

I have implemented ac# windows application. 我已经实现了ac#Windows应用程序。

For that application I want to provide login from windows user credentails. 对于该应用程序,我想提供Windows用户credentails的登录名。

Now the issue is I need to get the users list from windows pc through code. 现在的问题是我需要通过代码从Windows pc获取用户列表。

How to get users list eaither user created in winnt folder (under local users and groups), or user created in active directory domain. 如何使用户列出在winnt文件夹中创建的其他用户(在本地用户和组下),或在活动目录域中创建的用户。

I have an idea for get users list from winnt folder that is 我有一个想法从winnt文件夹中获取用户列表

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. 但是现在我想列出用户,如果该用户存在于活动目录或winnt文件夹中。

Any body give me code for this to cross verify users from both throug c# code 任何人都可以给我提供此代码,以便通过c#代码交叉验证用户

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. 对于Active Directory部分,如果使用的是.NET 3.5或更高版本,则可以查看新的System.DirectoryServices.AccountManagement命名空间。

You can use a PrincipalSearcher and a "query-by-example" principal to do your searching: 您可以使用PrincipalSearcher和“按示例查询”主体进行搜索:

// 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 如果您还没有,请绝对阅读MSDN文章.NET Framework 3.5中的管理目录安全性主体”,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement中的新功能。

For the local machine accounts, you can do basically the same thing - just with a different PrincipalContext : 对于本地计算机帐户,您可以执行基本相同的操作-只是使用不同的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! 其余代码是相同的-但是您的主体对象可能实际填充的属性要少得多(因为本地计算机帐户存储的信息不如Active Directory帐户那么多)-但是每个主体肯定都可以至少具有.Name属性!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为在Dev Server上使用C#.net的用户获取Active Directory域名时出错(在本地工作正常)? - Error while getting Active Directory domain name for users using C#.net on Dev Server (Works fine in local)? 当计算机不在活动目录中时,如何获取本地计算机组/用户列表? - How to get a list of local machine groups / users when machine is not in active directory? 在 .NET 中的 Active Directory 组中添加和删除用户 - Adding and removing users from Active Directory groups in .NET 获取嵌套活动目录组中的用户列表 - Get a list of users in nested active directory groups 如何使用C#从Active Directory中检索用户列表? - How do I retrieve a list of users from Active Directory using C#? 如何使用C#从Windows活动目录有效地获取用户列表 - How to efficiently obtain list of users from windows active directory using C# 列出活动目录域和子域中的用户 - List users in active directory domain AND subdomain 如何只从 Active Directory 中检索用户,而不是组 - How can I retrieve just users from the Active Directory, not groups WinNT://提供程序何时查询Active Directory? 或者如果是域帐户,如何获取本地组成员的SID - When does WinNT:// provider query Active Directory? Or how to get SID of local group member if it is domain account 如何通过域名搜索Active Directory用户? - How to search Active Directory users by their domain name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM