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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • Last Modified:

Directory Searcher Filter

I have a class that's getting the information I want but running too long

I THINK I need a filter on my Searcher.FindAll()

I want to look for a particular "user" based on thier login name


The FUnction is called with the following function and the "incomingUsername" is set with
HttpContext.Current.User.Identity.Name.ToString()


 Public Function getUserInfo(ByVal incomingUserName As String) As String
        getUserData = LoadADSIDetails("", incomingUserName)
        Return getUserData
    End Function

Following code starts a loop...
I want to apply a filter somehow

blah...blah...blah...
 Public Function LoadADSIDetails(ByVal Query As String, ByVal incomingUserName As String) As String

        Dim counter As Int16 = 0
        Dim searcher As New DirectorySearcher("")
        Dim DataToWrite As String = Nothing
        Try
            If Query.Trim.Length = 0 Then
                searcher.Filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(objectCategory=user))"
            Else
                searcher.Filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(objectCategory=user)(" + Query + "))"
            End If
            searcher.SearchScope = SearchScope.Subtree


            Dim FirstName, SurName, Email, UserName, GroupName As String

            For Each result As SearchResult In searcher.FindAll()

            blah...blah...blah...

Open in new window

0
lrbrister
Asked:
lrbrister
1 Solution
 
Julian123Commented:
Here's an example with a filter:

Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://my.domain.local";)
 Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
 Dim oresult As SearchResultCollection
 Dim result As SearchResult
 
osearcher.Filter = "(&(sAMAccountName=jsmith))"
 oresult = osearcher.FindAll
 
For Each result In oresult
  If Not result.GetDirectoryEntry.Properties("SAMAccountName").Value Is
 Nothing Then
   Response.Write(result.GetDirectoryEntry.Properties("SAMAccountName").Value
 & "<br />")
  End If
 Next
0
 
YZlatCommented:
what is Query?
0
 
lrbristerAuthor Commented:
Great!

Thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now