Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

i'm starting ASP.net with vb.net

in order to test scripts i have installed visual studio express 2k8 in addition to visual studio web developer 2k8 express

i try to connect to my ldap with this code:

VB
Imports System.DirectoryServices
Module Module1
    Sub Main()
GetDirectoryEntry()
    End Sub
    Public Function GetDirectoryEntry() As DirectoryEntry
        Dim de As DirectoryEntry = New DirectoryEntry()
        de.Path = "LDAP://"
        de.Username = "yourdomain\sampleuser"
        de.Password = "samplepassword"
        Return de
    End Function

End Module


from here

Could you tell me how i can see if there is connected with a text message like this: "AD connected" because i have always this Press any key to continue ...

Thanks

Kings Regards

Samuel
Posted

If I understand your question correctly, I would do it like this:
Public Function GetDirectoryEntry(ByVal Path As String) As DirectoryEntry
    Dim Result As DirectoryEntry = Nothing

    Try
        Result = New DirectoryEntry(Path)
        Dim Test As System.Guid = Result.Guid
    Catch ex As Exception
        Result.Close
        Result = Nothing
    End Try

    Return Result
End Function

Public Sub Main
    Dim DirEntry As DirectoryEntry = GetDirectoryEntry(Path)
    If DirEntry Is Nothing Then
        'The path is invalid
    Else
        'The path is valid
        'Use it
        DirEntry.Close
    End If
End Sub

If Path is valid, then the function will return an open, valid DirectoryEntry object. Otherwise, it will return Nothing.

Using Try/Catch blocks to determine valid values is bad form, but Active Directory doesn't seem to give us any other choice. Regardless of Path, you will get your DirectoryEntry even if the path was not found. The only way to know if the object is valid is to look at one of its values and fail. At least, that is my experience.

Update: :doh: The function is returning an open DirectoryEntry; it will need to be closed, otherwise you have a memory leak. I added the Dim statement to Main to hold the object returned by the function; do with it as you will.
 
Share this answer
 
v2
thanks Gregory.Gadow, you're fast :)

I will try
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900