Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AD DirectoryServices ADHelper User Exists

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi,

I have pinched some code from here to see if a user exists in AD.

I am new to VB.NET and having some difficulty in working out where ADHelper comes from.

I have an intellisense error stating ADHelper is not declared. I see that but I am not sure where it comes from for me to declare it.

If I remove ADHelper, I get a runtime error on
Code:
Dim results As SearchResultCollection = deSearch.FindAll()
Stating "The provider does not support searching and cannot search LDAP://RootDSE"

Here is the code:
Code:
Public Function UserExists(ByVal sUserName As String) As Boolean
        Dim de As DirectoryEntry = GetDirectoryEntry()
        Dim deSearch As DirectorySearcher = New DirectorySearcher()
        deSearch.SearchRoot = de
        deSearch.Filter = "(&(objectClass=user) (cn=" & sUserName & "))"
        Dim results As SearchResultCollection = deSearch.FindAll()
        If results.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

If anyone can shed some light on my issue I'd be most greatful.

Many thanks
W
 
Code:
    Public Function UserExists(ByVal sUserName As String) As Boolean
        Dim de As New DirectoryServices.DirectoryEntry
        Dim deSearch As New DirectoryServices.DirectorySearcher(de)

        deSearch.SearchRoot = de
        deSearch.Filter = "(&(objectClass=user) (cn=" & sUserName & "))"

        Dim results As DirectoryServices.SearchResultCollection = deSearch.FindAll()

        If results.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen,

Many thanks for the code.

It works without syntax error, however, it lies. If I use it to confirm if the user 'texctx1' exists, it returns false.

Do I need to tell it where in AD to search? I want it to search right from the top (rootDSE).

Many thanks.

A
 
Add setting to SearchScope.
[tt] deSearch.SearchScope = SearchScope.Subtree[/tt]
 
Also the actual format of the user name may be different than you think. For instance some companies might make you add the domain name in some part of the user name however in the AD it might not actually be there. Or vice versa.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Guys,

Apologies for the delay in replying.

I must be doing something wrong here as I still can't get it to recognise an existing username.

In the file module1.vb I have the following code:
Code:
Module Module1

    Public Sub checkUser()
        
        Dim sUsername As String
        sUsername = frmMain1.txtUsername.Text.ToString
        MessageBox.Show(sUsername)
        MessageBox.Show(UserExists(sUsername))
        If UserExists(sUsername) Then

            MessageBox.Show("User: " & sUsername & " found!")
        Else
            MessageBox.Show("User: " & sUsername & " not found!")
        End If

    End Sub

Public Function UserExists(ByVal sUserName As String) As Boolean
        Dim de As New DirectoryEntry
        Dim deSearch As New DirectorySearcher(de)

        deSearch.SearchRoot = de
        deSearch.Filter = "(&(objectClass=user) (cn=" & sUserName & "))"
        deSearch.SearchScope = SearchScope.Subtree
        Dim results As SearchResultCollection = deSearch.FindAll()

        If results.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
End Module

In form1.vb I have the following code:
Code:
Public Class frmMain1
    Private Sub btnTest2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest2.Click
        Module1.checkUser()

    End Sub
End Class

I'm not sure what I am doing wrong. Any pointers?

Are there any good books on writing VB applications, specifically for AD you'd recommend?

Many thanks

W
 
Put a RichTextBox on a windows form and name it RTB1. Then put the first name of the user you want to find. This should return every user that has that same first name. It could take a while depending on the name. Inversely you can try to see if it can simply find the name you used to long into windows by removing the quote and using the System.Enviroment.UserName option.

Then use this code.
Code:
    Private Sub ActiveDirectoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActiveDirectoryToolStripMenuItem.Click
        Dim SearchName As String = "Put First Name Here" 'System.Environment.UserName
        Dim entry As New DirectoryServices.DirectoryEntry()
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult
        Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection

        rtb1.Clear()

        mySearcher.Filter = ("(anr= " & SearchName & ")")

        For Each result In mySearcher.FindAll
            objCollResultProperty = result.Properties
            For Each obj As Object In objCollResultProperty
                For Each obj2 As Object In obj.Value
                    If TypeOf obj2 Is Array Then
                        For Each obj3 As Object In obj2
                            rtb1.Text &= "-> " & obj.key & " -- " & obj3.ToString & vbCrLf
                        Next
                    Else
                        rtb1.Text &= obj.key & " -- " & obj2.ToString & vbCrLf
                    End If
                Next
            Next
        Next

        MsgBox("Done")
    End Sub
This will output all the information you have access to for the user entered. You may get back more than you want, but it should at least help point you in the right direction.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
>Dim de As New DirectoryEntry
I am ready to grant all the benefit of the doubt, but what do you get out of this at any place after this line?
[tt] MessageBox.Show(de.Path)[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top