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