2011-12-10 36 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財產!