マイナーでもいいよね??

殆どVB系、でも .NET じゃない VB は知らないよん

目次

Blog 利用状況

書庫

日記カテゴリ

ユーザやグループの検索

.NET から Active Directory 内のデータにアクセスするために使用されるのは System.DirectoryServices 名前空間で、オブジェクトをカプセル化する DirectoryEntry クラスやクエリを実行する DirectorySearcher クラスなどがあります。

Active Directory 内のユーザやグループを検索する手順としては、

1. 検索のルート(起点)になる DirectoryEntry を作成する

2. 検索フィルタ(SQLでいう抽出条件)文字列を作成する

3. クエリを実行するための DirectorySearcher を作成する

4. クエリを実行して結果を取得する

5. 取得した結果から DirectoryEntry を取得する

とまぁこんな感じです。

 

1.については、ドメイン全体を検索するなら、ドメインの DirectoryEntry を作成します。

例えば、ドメイン内の全ユーザを検索する場合は次のようにします。

VB

Using root As New DirectoryEntry(LdapRootPath)    ‘---1.

  Dim filter = "(objectCategory=User)"    ‘---2.

  Using searcher As New DirectorySearcher(root, filter)    ‘---3.

    Using results = searcher.FindAll()    ‘---4.

      For Each res As SearchResult In results

        Using entry = res.GetDirectoryEntry()    ‘---5.

          ‘メールアドレスを取得するなら

          Dim mail = DirectCast(entry.Properties.Item("mail").Value, String)

        End Using

      Next

    End Using

  End Using

End Using

 

C#

using (var root = new DirectoryEntry(LdapRootPath))    //---1.

{

  var filter = "(objectCategory=User)";    //---2.

  using (var searcher = new DirectorySearcher(root, filter))    //---3.

  {

    using (var results = searcher.FindAll())    //---4.

    {

      foreach (SearchResult res in results)

      {

        using (var entry = res.GetDirectoryEntry())    //---5.

        {

          //メールアドレスを取得するなら

          var mail = (string)entry.Properties["mail"].Value;

        }

      }

    }

  }

}

 

LdapRootPath は LDAP(Lightweight Directory Access Protocol) のパスです。ドメインが abc.co.jp であれば "LDAP://DC=abc,DC=co,DC=jp" になります。

1.で指定したルートの DirectoryEntry と 2.で指定したフィルタ文字列は、3.で作成した DirectorySearcher のSearchRoot プロパティと Filter プロパティになります。

ドメイン内の全グループを検索する場合、フィルタ文字列は "(objectCategory=Group)" になります。

3.の DirectorySearcher の作成は、クエリテキストと SqlConnection を指定した SqlCommand の作成に似てます。

4.については、最初の1つだけ取得したい場合は、FindOne メソッドを使います。FindAll メソッドの戻り値の型は SearchResultCollection で、FindOne メソッドの戻り値の型は SearchResult です。これらのメソッドは SqlCommand の ExecuteReader メソッドと ExecuteScalar メソッドに似てます。

5.で検索した結果の SearchResult.GetDirectoryEntry メソッドで、検索したユーザやグループに対応する DirectoryEntry を取得してます。

投稿日時 : 2013年6月26日 23:50

コメントを追加

# Why viewers still make use of to read news papers when in this technological world all is accessible on web? 2022/06/12 5:17 Why viewers still make use of to read news papers

Why viewers still make use of to read news papers when in this
technological world all is accessible on web?

# helping writing essay d265pg 2022/09/08 17:40 Charlosmox

You mentioned it effectively! https://definitionessays.com/ essay writing service review

# good phd proposal c498zc 2023/02/10 16:44 Albertosed


Position effectively regarded!!
https://essaywritingservicelinked.com/ defending thesis

# buy essay cheap online a98sqa 2023/02/26 16:21 CharlesSnoff


Nicely put, Thanks a lot!
pay for someone to write your essay https://quality-essays.com/ pay for my essay

# custom paper service p82syx 2023/03/01 7:06 StevenGrelo


Thanks. I like this.
online paper writing services https://service-essay.com/ buy cheap papers online

# assessment of service delivery thesis y78wtf 2023/03/03 5:09 Josephbried


Regards, Loads of posts!
thesis service in india https://writingthesistops.com/ write a thesis statement for your argument on community service

# descriptive essay help v74efx 2023/03/07 1:23 EugeneSib


Reliable write ups. Appreciate it!
writing an abstract for a dissertation https://buycheapessaysonline.com college personal statement essay https://essaywritingservicebbc.com

# best resume writing services nj w47xwv 2023/03/08 7:13 Gregorysaipt

You actually stated this wonderfully!
college essay diversity https://dissertationwritingtops.com help writing a essay https://researchpaperwriterservices.com

# phd proposal writing help q29btw 2023/03/08 8:25 EugeneSib


Helpful content. Thanks!
writing reflective essays https://dissertationwritingtops.com essay review service https://essaywritingservicehelp.com

# buy custom essay c721vq 2023/03/09 0:34 EugeneSib


Seriously loads of very good info!
dissertation and theses https://researchpaperwriterservices.com essay on helping others https://dissertationwritingtops.com

# high school essay writing e12arc 2023/03/09 15:59 EugeneSib


Terrific material, Regards.
cuny college essay https://writingthesistops.com best college essay writing services https://dissertationwritingtops.com

# research and writing services c12niq 2023/03/10 2:46 Gregorysaipt


Valuable postings. Kudos.
best essay for you https://helpwithdissertationwriting.com essays on writing https://writingpaperforme.com

# custom writing bay j48ede 2023/03/10 6:31 EugeneSib


Wonderful posts. Thanks.
can you write my essay for me https://customthesiswritingservice.com online cv writing services https://writingpaperforme.com

# law essays j35voy 2023/03/10 20:53 EugeneSib


Amazing tons of superb material.
essay thesis https://essaywritingservicehelp.com things to write a compare and contrast essay on https://essaywritingservicebbc.com

# business dissertation topics n69qpy 2023/03/11 0:22 Gregorysaipt


Regards! An abundance of tips.
how write a essay https://bestpaperwritingservice.com narrative writing essays https://essaywriting4you.com

# writing essay about yourself t117yc 2023/03/11 22:07 Gregorysaipt


Cheers! An abundance of stuff.
how to write essay for college application https://paperwritingservicecheap.com essay writing skill https://helpwithdissertationwriting.com

# help writing a book e33qsz 2023/03/12 3:11 EugeneSib


Wonderful tips. Thanks a lot!
best freelance writer websites https://essaywritingserviceahrefs.com buy essay papers https://custompaperwritersservices.com

# dissertation write up x70fsm 2023/03/13 8:20 EugeneSib


You have made your point!
online help with essay writing https://essaywriting4you.com dissertation proposal writing service https://service-essay.com

# help writing college application essay d79ttt 2023/03/13 17:36 Gregorysaipt


Truly all kinds of terrific tips.
writing an informational essay https://custompaperwritersservices.com supplemental college essays https://quality-essays.com

# essay about writing experience q527to 2023/04/01 12:28 EugeneSib


Amazing advice. Thanks a lot!
college essay music https://customthesiswritingservice.com essay homework help https://dissertationwritingtops.com

# The plugins developed for WordPress 2023/05/10 0:27 Justas

The plugins developed for WordPress serve to enhance the features and functions of a WordPress website, allowing you to build your awesome and functional site https://t.me/wpigaming/648 Customise WordPress with powerful, professional and intuitive fields.

タイトル  
名前  
URL
コメント