Link to home
Start Free TrialLog in
Avatar of T Hoecherl
T HoecherlFlag for United States of America

asked on

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
Avatar of kevinhigg
kevinhigg

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!
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

Avatar of T Hoecherl

ASKER

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
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.
Avatar of yo_bee
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

Thank you sammySeltzer. You are correct.  System.Web.Hosting is not available in a win forms application.
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
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

Did you change the ComboBox to your listBox
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
ASKER CERTIFIED SOLUTION
Avatar of yo_bee
yo_bee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you yo_bee.  Working now.  I wish I could award more than 500 points.

T
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.