本文介绍了如何列出使用C#.NET从PC要么他在WINNT文件夹中创建用户(本地用户和组)或Active Directory(在域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个C#Windows应用程序。

I have implemented a c# windows application.

有关的应用程序,我想从Windows用户credentails提供登录。

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

现在的问题是我需要从Windows PC上的用户列表,通过code。

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

如何让用户列出WINNT文件夹中创建eaither用户(在本地用户和组)在Active Directory域中创建,或用户。

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

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);
} 

但现在我想列出的用户,如果他是存在于Active Directory或Winnt文件夹。

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

任何机构给我code这个跨越验证来自通过量的C#code用户

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

推荐答案

有关活动目录的一部分,如果你使用.NET 3.5或更高版本,你可以看看新的的System.DirectoryServices .AccountManagement 命名空间。

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.

您可以使用 PrincipalSearcher 和查询通过例如主要做你的搜索:

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);
    }
}

如果您还没有 - 绝对阅读MSDN文章管理目录安全主体在.NET Framework 3.5 这很好地说明如何使新功能的最佳使用 System.DirectoryServices.AccountManagement

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

有关本地计算机帐户,你可以做的基本上是同样的事情 - 只是用不同的 PrincipalContext

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);

的code,其余是相同的 - 但你的主要对象可能有很多实际填充的属性更少(因为本地计算机帐户没有保存为他们的Active Directory帐户尽可能多的信息) - 但每次本金肯定确实有至少一个 .Name点属性

这篇关于如何列出使用C#.NET从PC要么他在WINNT文件夹中创建用户(本地用户和组)或Active Directory(在域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:39