[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DirectoryService Searcher.FindAll error

Posted on 2012-09-14
15
Medium Priority
?
1,745 Views
Last Modified: 2013-12-24
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
Comment
Question by:thoecherl
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:kevinhigg
ID: 38402429
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
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 38402603
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
 

Author Comment

by:thoecherl
ID: 38406469
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 29

Expert Comment

by:sammySeltzer
ID: 38408068
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
 
LVL 24

Expert Comment

by:yo_bee
ID: 38409353
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
 

Author Comment

by:thoecherl
ID: 38409953
Thank you sammySeltzer. You are correct.  System.Web.Hosting is not available in a win forms application.
0
 

Author Comment

by:thoecherl
ID: 38409973
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
 
LVL 24

Expert Comment

by:yo_bee
ID: 38409994
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
 
LVL 24

Expert Comment

by:yo_bee
ID: 38410123
Did you change the ComboBox to your listBox
0
 

Author Comment

by:thoecherl
ID: 38410125
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
 
LVL 24

Accepted Solution

by:
yo_bee earned 2000 total points
ID: 38410164
Change where ever you see CN to sAMAccountName

This is case sensitive
0
 

Author Closing Comment

by:thoecherl
ID: 38410328
Thank you yo_bee.  Working now.  I wish I could award more than 500 points.

T
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 38410359
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question