Enumerating users w/ VB.net

Enumerating users w/ VB.net

Post by David » Wed, 02 Jul 2003 07:55:03



I am trying to list all the users in my forest in a drop
down list.  I have users in many different OU's.  Using
the code below I have been able to get the users in the
Users container, but I can not figure out what I need to
do to get users in other OU's.  I have tried replacing
CN=users with OU=marketing, but it does not work.

Does anyone know of a good referenece for accessing active
directory or WMI with vb.net?  I have not been able to
find a good resource online.  Any help is appreciated.  

Private Sub enumerateUsers()

  Dim entry As New DirectoryEntry
    ("LDAP://internal.myCompany.com/CN=users,
    DC=internal, DC=myCompany, DC=com", "Username",
    "password")

  Dim mySearcher As New DirectorySearcher(entry)
  mySearcher.PropertiesToLoad.Add("CN")
  mySearcher.Sort.PropertyName = "CN"

  Dim results As SearchResultCollection =
     mySearcher.FindAll()

  Dim result As SearchResult
  Dim username As String

  For Each result In results

    ddUsers.Items.Add(New ListItem(result.Properties
      ("CN")(0)))

  Next result

End Sub

 
 
 

Enumerating users w/ VB.net

Post by MVP - ADS » Wed, 02 Jul 2003 13:04:27


You can search the entire domain by properly setting the root for your
search and specifying a Subtree scope.  The domain search root can be
determined dynamically by binding to the RootDSE object and reading the
defaultNamingContext attribute:

Dim rootEntry as New DirectoryEntry("LDAP://yourserver.com/RootDSE",
"username", "password")
Dim root as String =
DirectCast(rootEntry.Properties("defaultNamingContext").Value,
System.String)

dim entry as New DirectoryEntry("LDAP://yourserver.com/" + root, "username",
"password")

To search for only user objects, you should create a search filter to do
that:

mySearcher.Filter = "(&(objectCategory=person)(objectClass=user))"

If there will be more than 1000 users to find, you must use a paged search
to get all of the results.  Set the PageSize property to a number > 0 and <=
1000:

mySearcher.PageSize = 1000

If you need to search the entire forest at once instead of just a single
domain you can do so by switching to a global catalog server and  the GC
provider.

There is some really helpful documentation from MS on S.DS here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netd...

HTH,

Joe K.


Quote:

> I am trying to list all the users in my forest in a drop
> down list.  I have users in many different OU's.  Using
> the code below I have been able to get the users in the
> Users container, but I can not figure out what I need to
> do to get users in other OU's.  I have tried replacing
> CN=users with OU=marketing, but it does not work.

> Does anyone know of a good referenece for accessing active
> directory or WMI with vb.net?  I have not been able to
> find a good resource online.  Any help is appreciated.

> Private Sub enumerateUsers()

>   Dim entry As New DirectoryEntry
>     ("LDAP://internal.myCompany.com/CN=users,
>     DC=internal, DC=myCompany, DC=com", "Username",
>     "password")

>   Dim mySearcher As New DirectorySearcher(entry)
>   mySearcher.PropertiesToLoad.Add("CN")
>   mySearcher.Sort.PropertyName = "CN"

>   Dim results As SearchResultCollection =
>      mySearcher.FindAll()

>   Dim result As SearchResult
>   Dim username As String

>   For Each result In results

>     ddUsers.Items.Add(New ListItem(result.Properties
>       ("CN")(0)))

>   Next result

> End Sub


 
 
 

1. Need to Authenticate against OpenLDAP Server and enumerate objects with DirectoryServices Class in VB.NET

Hi All,

Does anyone have any sample code in VB.NET, using the
DirectoryServices Class that will allow me to authenticate to a
NON-ACTIVE DIRECTORY LDAP Server such as OpenLDAP or iPlanet and then
enumerate objects based on a search filter?  Code Samples seem to be
few and far between.  I've also heard that it was not without its
"pain" using ADSI and VB6.  Are there any known issues using this
class against a third-Party LDAP server?  Which authentication types
are supported?  (I.e Is Anonymous bind the only thing supported?  Can
you browse custom attributes or schema extensions?)  Any known
"gotchas" or confirmed bugs?

Here is what I'd like to do:  (Sample Code would be GREATLY
appreciated! :-))

1.  The LDAP server is ldapserver.domain.com
2.  The Top Level Organization: O=Employees
3.  uid=test, userPassword=password
4.  Bind to the directory with the aforementioned credentials.
5.  Enumerate all of the objects in O=Employees container where
objectClass=inetOrgPerson
6.  Put the enumerated objects into a listbox (VB Form)

I have no problem when attempting this against Active Directory, but
I'm getting all sorts of weird errors ranging from Access Denied to
Unknown Handle when attempting to do this against OpenLDAP.  Any
thoughts, suggestions, and sample VB.NET code would be greatly
appreciated!  Thanks in advance for all your help.

- Dilbert

2. MS Mail and DEC Allin1

3. set password using asp.net w/ vb.net?

4. Saving settings in Describe 5

5. How do I use paging in VB.NET & ADO.NET using OLEDB Provider ADsDSOObject

6. Ndis miniport Rx: Lookahead buffer ?

7. List of users and groups in vb.NET

8. FS: SM124 Mono Monitor.

9. add user vb.net

10. New User via VB.NET

11. how can I search for a user in vb.net with domain name and accountname?

12. creating a user with vb.net

13. Getting user details (VB.NET)