2011-12-10 33 views
0

在winnt文件夹(本地用户和组)或活动目录(在域下)创建的用户从PC中列出了我已经实现了一个C#窗口应用程序。如何使用c#.net

对于那个应用程序,我想提供从Windows用户credentails登录。

现在的问题是我需要通过代码从windows pc获取用户列表。

如何获取用户列表在winnt文件夹(本地用户和组下)中创建的用户,或在Active Directory域中创建的用户。

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

但现在我要列出用户,如果他是在Active Directory或WINNT文件夹中。

任何机构给我的代码,这个跨越来自通过量的C#代码

+1

你能给我一百万美元吗?这不是'给我代码'的地方。 – Bakudan

+0

为什么你需要用户列表?如果用户正在启动您的应用程序,那么他们将会被验证,您可以轻松获得他们的身份。 –

+0

@Raheem看看http://stackoverflow.com/questions/5058261/how-to-get-update-contacts-within-active-directory –

回答

1

为Active Directory部分验证的用户,如果你正在使用.NET 3.5或更新版本,你可以看看新System.DirectoryServices.AccountManagement命名空间。

您可以使用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); 
    } 
} 

如果您尚未 - 绝对看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5这很好地说明如何使在System.DirectoryServices.AccountManagement

物尽其用的新功能对于本地计算机帐户,就可以基本上做同样的事情 - 只是用不同的PrincipalContext

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

其余的代码是相同的 - 但是您的主体对象可能有更少的实际填充的属性(因为本地计算机帐户没有为Active Directory帐户存储的信息) - 但每个本金肯定至少有一个.Name财产!