T Hoecherl
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=Dom ain.local; CN=Users")
Domain1.Username = "Domain\Admin"
Domain1.Password = "password"
Dim Searcher1 As DirectorySearcher = New DirectorySearcher("(&(obje ctCategory =Person)(o bjectClass =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).Pro perties("s amaccountn ame")(0).T oString())
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.D irectorySe rvicesCOME xception (0x80072020): An operations error occurred.
I have searched the internet groups, but I cannot find a solution. Can someone help?
T
Private Sub CreateAD_List()
Dim Domain1 As DirectoryEntry = New DirectoryEntry()
Domain1.Path = ("LDAP://10.0.0.229/DC=Dom
Domain1.Username = "Domain\Admin"
Domain1.Password = "password"
Dim Searcher1 As DirectorySearcher = New DirectorySearcher("(&(obje
Dim Lista1 As New System.Collections.Generic
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).Pro
lstUsers.Items.Add(Lista1(
Next
End Sub
When I run the application, it errors on this line: Results1 = Searcher1.FindAll(). The error is
System.DirectoryServices.D
I have searched the internet groups, but I cannot find a solution. Can someone help?
T
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
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
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.
using System.Web.Hosting;
but I am not sure that this is supported in win forms.
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
ASKER
Thank you sammySeltzer. You are correct. System.Web.Hosting is not available in a win forms application.
ASKER
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=compute r)(objectC ategory=co mputer)(us erAccountC ontrol:1.2 .840.11355 6.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
objSearch.Filter = "(&(objectCategory=compute
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 =
ObjSearch.Filter =
You will want to change the
objSearch.Filter =
"(&(objectCategory=computer)(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=0))"
to ObjSearch.Filter =
"(&(objectCategory=Person)(objectClass=user))"
Did you change the ComboBox to your listBox
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you yo_bee. Working now. I wish I could award more than 500 points.
T
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.
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.
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):
Open in new window
Best of luck!