• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1854
  • Last Modified:

DirectoryService Searcher.FindAll error

I have a vb.net Windows forms application in which I am trying to fill a combo box with a list of user login id's from Active Directory.  Here is my code:

    Private Sub CreateAD_List()
        Dim Domain1 As DirectoryEntry = New DirectoryEntry()
        Domain1.Path = ("LDAP://10.0.0.229/DC=Domain.local;CN=Users")
        Domain1.Username = "Domain\Admin"
        Domain1.Password = "password"
        Dim Searcher1 As DirectorySearcher = New DirectorySearcher("(&(objectCategory=Person)(objectClass=user)")
        Dim Lista1 As New System.Collections.Generic.List(Of String)()
        Searcher1.SearchRoot = Domain1
        Searcher1.SearchScope = SearchScope.Subtree
        Dim Results1 As SearchResultCollection
        Results1 = Searcher1.FindAll()

        For i As Integer = 0 To Results1.Count
            Lista1.Add(Results1(i).Properties("samaccountname")(0).ToString())
            lstUsers.Items.Add(Lista1(i))
        Next
       
    End Sub

When I run the application, it errors on this line:  Results1 = Searcher1.FindAll().  The error is
System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operations error occurred.

I have searched the internet groups, but I cannot find a solution.  Can someone help?

T
0
T Hoecherl
Asked:
T Hoecherl
  • 5
  • 5
  • 2
  • +1
1 Solution
 
kevinhiggCommented:
I think that your search path may be slightly off.  Normally this would be something like:

Domain1.Path = ("LDAP://10.0.0.229/CN=Users,DC=Domain.local")

Open in new window


Or even like this if you're going to let name resolution take care of the DC choice (if you have more than one, this may be a good idea for availability):

Domain1.Path = ("LDAP://CN=Users,DC=Domain.local")

Open in new window


Best of luck!
0
 
sammySeltzerCommented:
Try this:

    Private Sub CreateAD_List()
        Dim Domain1 As DirectoryEntry = New DirectoryEntry()
        Domain1.Path = ("LDAP://10.0.0.229/DC=Domain.local;CN=Users")
        Domain1.Username = "Domain\Admin"
        Domain1.Password = "password"
        Dim Searcher1 As DirectorySearcher = New DirectorySearcher("(&(objectCategory=Person)(objectClass=user)")
        Dim Lista1 As New System.Collections.Generic.List(Of String)()
        Searcher1.SearchRoot = Domain1
        Searcher1.SearchScope = SearchScope.Subtree
        Dim Results1 As SearchResultCollection

Using HostingEnvironment.Impersonate()
        Results1 = Searcher1.FindAll()
End Using


        For i As Integer = 0 To Results1.Count
            Lista1.Add(Results1(i).Properties("samaccountname")(0).ToString())
            lstUsers.Items.Add(Lista1(i))
        Next
       
    End Sub

Open in new window

0
 
T HoecherlDeveloperAuthor Commented:
sammySeltzer,

Your posting is encouraging because all of the research I have done seems to point to impersonation.  But I haven't been able to figure out how to do it.  It seems that HostingEnvironment is in the System.Web.dll, but I believe that is for asp.net or other web applications, isn't it?  This is a win forms application, not web forms.  I added a reference to System.Web.Services, but there is no reference available in VS 2010 for System.Web.  In short, I can't find a HostingEnvironment namespace to import.  Can you help me with that?

T
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
sammySeltzerCommented:
If this were a web app, then you would have added this:

using System.Web.Hosting;

but I am not sure that this is supported in win forms.
0
 
yo_beeDirector of Information TechnologyCommented:
Here is my code snippet I use in VB application

 Private Sub SearchAD()

        Dim objRootDSE
        Dim objSchemaContainer
        Dim strSchemaPath
        'Get the Root DSE from a random DC
        objRootDSE = GetObject("LDAP://RootDSE")
        'Get the Schema NC path for the domain
        strSchemaPath = objRootDSE.Get("defaultNamingContext")
        'Connect to the schema container on a random DC
        objSchemaContainer = GetObject("LDAP://" & strSchemaPath)



        Dim objSearch As New DirectorySearcher()
        objSearch.SearchRoot = New DirectoryEntry(objSchemaContainer)
        objSearch.Filter = "(&(objectCategory=computer)(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=0))"
        objSearch.SearchScope = SearchScope.Subtree
        objSearch.PropertiesToLoad.Add("cn")
        Dim colQueryResults As SearchResultCollection
        colQueryResults = objSearch.FindAll()
        Dim objResult As SearchResult
        For Each objResult In colQueryResults
            ComboBox2.Items.Add(objResult.Properties("cn")(0))
        Next

    End Sub

Open in new window

0
 
T HoecherlDeveloperAuthor Commented:
Thank you sammySeltzer. You are correct.  System.Web.Hosting is not available in a win forms application.
0
 
T HoecherlDeveloperAuthor Commented:
Thank you yo_bee.  Your snippet has eliminated the error, but I'm still not getting the AD user list.  I don't understand this line:

objSearch.Filter = "(&(objectCategory=computer)(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=0))"

The number you are using with the userAccountControl argument -- is that the server IP address?  If not, what is it and how can I find it in my environment?

Thanks again for responding.  I feel now like I am inches away from a solution.

T
0
 
yo_beeDirector of Information TechnologyCommented:
My snippet is for the computers.
You will want to change the

objSearch.Filter =
"(&(objectCategory=computer)(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=0))" 

Open in new window

to


ObjSearch.Filter =
"(&(objectCategory=Person)(objectClass=user))"

Open in new window

0
 
yo_beeDirector of Information TechnologyCommented:
Did you change the ComboBox to your listBox
0
 
T HoecherlDeveloperAuthor Commented:
Thank you yo_bee.  I realize now that the UAC argument is identifying OID for the LDAP_MATCHING_RULE_BIT_AND.  Thanks for the correction.

With the correction, I am now able to get a list of users.  However, the list is the user names.  I need the logon IDs.  Is there a way to do that?

Thanks.

T
0
 
yo_beeDirector of Information TechnologyCommented:
Change where ever you see CN to sAMAccountName

This is case sensitive
0
 
T HoecherlDeveloperAuthor Commented:
Thank you yo_bee.  Working now.  I wish I could award more than 500 points.

T
0
 
yo_beeDirector of Information TechnologyCommented:
All good.
Glad to help.
You probably should have awarded multiple solutions because the snippet is what really helped you, but the two other replies were subsequent to that reply
This helps others when search the KB to find a solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now