2011-12-10 19 views
0

私はC#windowsアプリケーションを実装しました。c#.netを使用してwinntフォルダ(ローカルユーザとグループ)またはアクティブディレクトリ(ドメイン)で作成したPCからユーザをリストする方法

このアプリケーションでは、Windowsユーザーのクレデンシャルのログインを提供したいと考えています。

今問題は、Windowsのコードからユーザーのリストを取得する必要があることです。

ユーザを取得する方法は、ローカルユーザとグループの下の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); 
} 

である。しかし、彼は、アクティブディレクトリまたはWinntフォルダに存在している場合、今、私は、ユーザーを一覧表示したいWINNTフォルダからユーザーの一覧を取得するためのアイデアを持っています。

任意の体は私に、これは、Active Directoryの部分については、両方のC#コードthroug

+1

あなたは私に100万ドルを払えますか?これは '私にコードを渡す '場所ではありません。 – Bakudan

+0

なぜユーザーの一覧が必要ですか?ユーザーがアプリケーションを起動している場合、ユーザーは既に認証されており、簡単にそのIDを取得できます。 –

+0

@Raheem http://stackoverflow.com/questions/5058261/how-to-get-update-contacts-within-active-directoryを見てください –

答えて

1

からユーザーを検証する交差する、とあなたは.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のプロパティを持っています!

関連する問題