2016-05-16 9 views
0

System.DirectoryServices.AccountManagementを使用している組織で電子メールアドレスを持つすべてのユーザーを返す方法を理解しようとしていますが、ドメインを指定する必要はありません。System.DirectoryServices.AccountManagementを使用して組織内のすべてのユーザーを検索する一般的な方法?

これは、組織内のサーバーPCにインストールされるWindowsサービスで使用され、理想的には、各クライアントがインストール時に各自のドメインに入力する必要はありません。

現在、私は次のようにします。

context = New PrincipalContext(ContextType.Domain) 

Using userPrin As New UserPrincipal(context) 
    userPrin.Enabled = True 
    userPrin.EmailAddress = "*" 
    Using searcher = New PrincipalSearcher(New UserPrincipal(context)) 
     searcher.QueryFilter = userPrin 

     Using results As PrincipalSearchResult(Of Principal) = searcher.FindAll 
      Trace.WriteLine("results.count: " & results.Count) 
      userPrincipalResult = (From r In results Select TryCast(r, UserPrincipal)) 
      Trace.WriteLine("userPrincipalResult.Count: " & userPrincipalResult.Count) 
      userList = (From cr In userPrincipalResult Select cr.EmailAddress).ToList() 
     End Using 

    End Using 
End Using 

これに伴う問題は、メインドメインが@client.comであるが、彼らは同じフォレスト内の別のドメイン@clientdifferent.comを持っている場合、それはから任意のユーザーを返さないということですこの他のドメイン。

私は、LDAPの代わりにGCを検索してより高いレベルで検索できることを説明した記事を見つけましたが、ドメインとポート番号を追加する必要があります。ドメインを指定しなくても同様のことをする方法はありますか?

リンク:

How to search in multiple domains using System.DirectoryServices.AccountManagement?

When do I need a Domain Name and a Domain Container to create a PrincipalContext?

C# - Searching for users across multiple Active Directory domains

答えて

0

うと、この作品のようなもの:

Using tempForest = ActiveDirectory.Forest.GetCurrentForest() 
    For Each domain As ActiveDirectory.Domain In tempForest.Domains 
     context = New PrincipalContext(ContextType.Domain, domain.Name) 
     Using userPrin As New UserPrincipal(context) 
      userPrin.Enabled = True 
      userPrin.EmailAddress = "*" 
      Using searcher = New PrincipalSearcher(New UserPrincipal(context)) 
       searcher.QueryFilter = userPrin 

       Using results As PrincipalSearchResult(Of Principal) = searcher.FindAll 
        Trace.WriteLine("results.count: " & results.Count) 
        userPrincipalResult = (From r In results Select TryCast(r, UserPrincipal)) 
        Trace.WriteLine("userPrincipalResult.Count: " & userPrincipalResult.Count) 
        userList = (From cr In userPrincipalResult Select cr.EmailAddress).ToList() 
       End Using 

      End Using 
     End Using 
    Next 
End Using 
関連する問題