18

.NETで使用しているsn名前()と名( )でADのユーザーを検索しようとしています。givenNameDirectorySearcher

sAMAccountname私はこのコードに基づいてユーザーを見つけることができます:

 DirectorySearcher searcher1 = new DirectorySearcher(entry);
 searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(SAMAccountname={0}))",aLogin);

 SearchResult results1;
 results1 = searcher1.FindOne();

しかし、私がそれをしようとするgivenNamesn

DirectorySearcher searcher1 = new DirectorySearcher(entry);
searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1})", aName, aSName);

SearchResultCollection results1;
results1 = searcher1.FindAll();

それは機能しません。メッセージには「無効なフィルター」と表示されます。givenNameとに基づいてフィルタリングできませんsnか?

どうすればこれを達成できますか?

4

3 に答える 3

31

.NET 3.5以降を使用している場合は、PrincipalSearcherおよび「例によるクエリ」プリンシパルを使用して検索を行うこともできます。

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
qbeUser.Surname = "Miller";

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

まだ読んでいない場合は、MSDNの記事「.NETFramework3.5でのディレクトリセキュリティプリンシパルの管理」を絶対に読んでください。この記事では、の新機能を最大限に活用する方法を説明していますSystem.DirectoryServices.AccountManagement。または、System.DirectoryServices.AccountManagement名前空間に関するMSDNドキュメントを参照してください。

もちろん、必要に応じて、作成する「例によるクエリ」ユーザープリンシパルに他のプロパティを指定することもできます。

  • DisplayName(通常:名+スペース+姓)
  • SAM Account Name-Windows/ADアカウント名
  • User Principal Name-「username@yourcompany.com」スタイル名

の任意のプロパティを指定して、UserPrincipalそれらをの「例によるクエリ」として使用できますPrincipalSearcher

于 2012-03-07T15:22:13.297 に答える
13

フィルタに閉じ括弧がありません。試す:

searcher1.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", aName, aSName);
于 2012-03-07T15:09:02.163 に答える
0

これはエラーではありません。

忘れた)

于 2012-03-07T15:10:12.490 に答える