この種のタスクを実行するには、 System.DirectoryServices 名前空間を使用します( DirectoryServicesはLDAP用の管理されたラッパーです)。
Try
' Bind to the users container.
Dim entry As New
DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com")
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Create a SearchResultCollection object to hold a collection
' of SearchResults returned by the FindAll method.
Dim result As SearchResultCollection = mySearcher.FindAll()
' Create an object to hold a single result from the
' result collection.
Dim resEnt1 As SearchResult
' Get search results. For more information,
' see Getting Search Results.
'
' This sample uses Try...Catch to catch errors.
' Create an Exception object. For more information,
' see System.Exception.
Catch Exception1 As System.Runtime.InteropServices.COMException
Console.WriteLine(Exception1.Message)
Catch Exception2 As InvalidOperationException
Console.WriteLine(Exception2.Message)
Catch Exception3 As NotSupportedException
Console.WriteLine(Exception3.Message)
End Try
You can use a search string such as "(&(objectCategory=user)(objectClass=person)(sAMAccountName=" + userId + "))"
to search for a user (userID would need to be replaced with the User's ID).
すべてをまとめて結合するには、以下のスニペットを修正してユーザーのすべてのプロパティを表示します。ユーザー名にのみフォーカスするように調整できます。
Dim results As SearchResultCollection = Nothing
Try
' Bind to the users container.
Dim path As String = "LDAP://CN=users,DC=fabrikam,DC=com"
path = "LDAP://CN=Users,DC=strohmadom,DC=nttest,DC=microsoft,DC=com"
Dim entry As New DirectoryEntry(path)
' Create a DirectorySearcher object.
Dim mySearcher As New DirectorySearcher(entry)
' Set a filter for users with the name test.
mySearcher.Filter = "(&(objectClass=user)(anr=test*))"
' Use the FindAll method to return objects to a SearchResultCollection.
results = mySearcher.FindAll()
' Iterate through each SearchResult in the SearchResultCollection.
Dim searchResult As SearchResult
For Each searchResult In results
' Display the path of the object found.
Console.WriteLine("Search properties for {0}", _
searchResult.Path)
' Iterate through each property name in each SearchResult.
Dim propertyKey As String
For Each propertyKey In searchResult.Properties.PropertyNames
' Retrieve the value assigned to that property name
' in the ResultPropertyValueCollection.
Dim valueCollection As ResultPropertyValueCollection = searchResult.Properties(propertyKey)
' Iterate through values for each property name in each SearchResult.
Dim propertyValue As Object
For Each propertyValue In valueCollection
' Handle results. Be aware that the following
' WriteLine() only returns readable results for
' properties that are strings.
Console.WriteLine("{0}:{1}", _
propertyKey, _
propertyValue.ToString())
Next propertyValue
Next propertyKey
Next searchResult
Finally
' To prevent memory leaks, always call
' SearchResultCollection.Dispose() manually.
If Not results Is Nothing Then
results.Dispose()
results = Nothing
End If
End Try