locked
Connecting to LDAP Server Using ASP.NET RRS feed

  • Question

  • User1374550647 posted

    You can use the following function to connect to LDAP server using ASP.NET. This function takes three parameters; the LDAP server name to which you want to connect and the username and password to login with.

    I have defined a boolean variable as a return value. oRoot is a directory entry that is used to take a server name.

    My LDAP server is used as an E-mail account management server. By oResult you can search for users matching your needs. You can use this function to list all results matched, or to match only any one matched.

    Do not forget to import System.DirectoryServices library.

    Public Function ConnectToLDAP(ByVal ldapServerName As String, ByVal username As String, ByVal password As String) As Boolean

            Dim foundUser As Boolean
            Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
                                     "/ou=People,o=ist.edu.jo,dc=ist,dc=edu,dc=jo")

            Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
            Dim oResults As SearchResultCollection
            Dim oResult As SearchResult

            Try

                foundUser = False
                oRoot.AuthenticationType = AuthenticationTypes.FastBind
                oRoot.Username = "uid=" + username + ",ou=people,o=just.edu.jo,dc=just,dc=edu,dc=jo"
                oRoot.Password = password

                oSearcher.Filter = "(&(objectclass=*)(uid=" & username & "))"
                oResults = oSearcher.FindAll

                For Each oResult In oResults
                    foundUser = True
                Next

            Catch ex As Exception

            End Try

            Return foundUser
        End Function

    Tuesday, January 22, 2008 4:44 AM

All replies

  • User1739017382 posted

    Hi mohammed,

    Your code is absolulety right.

    I was successfully able to connect with the LDAP Server from your code.

    Thanking you.

     

    Tuesday, January 27, 2009 6:32 AM
  • User1463673714 posted

    Hi Mohammed,

    I am getting Logon failure unknown username or password at the below line

     

    oSearcher.Filter = "(&(objectclass=*)(uid=" & username & "))"

    Friday, May 29, 2009 8:41 AM
  • User1374550647 posted

    You have to check if the username and password used in the access of LDAP are correct or not.

    The username and password could be any user credentials on the active directory or the system administrator credentials.

     

    Monday, June 1, 2009 3:41 AM
  • User564060654 posted

    Thaaaaaaaaanx alot for the code,, it works very will with me Laughing

    actually I searched alot for such useful one,, but nothing works with me till i found this perfect sample.

    it  rescued my application !!! Tongue out

    thanx again

     

    Thursday, October 8, 2009 8:05 AM
  • User-1906420162 posted

    Hi,

     This doesnt works for me. Displays "Your login attempt was not successful. Please try again." Will you please tell me the ADAM configuration.Very urgent please help me

    Wednesday, October 14, 2009 8:50 AM
  • User1374550647 posted

    I think that your error message: "Your login attempt was not successful. Please try again." is not from LDAP. Please try this article from the following link:

    http://www.codeproject.com/KB/aspnet/LoginControlError.aspx


    Thursday, October 15, 2009 12:50 AM
  • User1619153493 posted

    Every Thing is fine in the above code but in the below line have some mistake with me.

     Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _

                                     "/ou=People,o=ist.edu.jo,dc=ist,dc=edu,dc=jo")


    Below code is working fine with me. 

             Dim foundUser As Boolean    

            Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/ou=People,dc=atul-runthala,dc=ac,dc=in")

            Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)

            Dim oResults As SearchResultCollection

            Dim oResult As SearchResult

            Try

                foundUser = False

                oRoot.AuthenticationType = AuthenticationTypes.FastBind

                oRoot.Username = "uid=" + username + ",ou=people,dc=atul-runthala,dc=ac,dc=in"

                oRoot.Password = password


                oSearcher.Filter = "(&(objectclass=*)(uid=" & username & "))"

                oResults = oSearcher.FindAll


                For Each oResult In oResults

                    foundUser = True


                    Dim test As String

                    test = ""

                    test = "Test Ok"

                    Response.Write(test)

                Next


            Catch ex As Exception

                Response.Write(ex.Message)

            End Try

            Return foundUser

    Dim foundUser As Boolean
            'Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/ou=People,o=bits-pilani.ac.in,dc=bits-pilani,dc=ac,dc=in")
            Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/ou=People,dc=bits-pilani,dc=ac,dc=in")
            Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
            Dim oResults As SearchResultCollection
            Dim oResult As SearchResult
            Try
                foundUser = False
                oRoot.AuthenticationType = AuthenticationTypes.FastBind
                ' oRoot.Username = "uid=" + username + ",ou=people,o=bits-pilani.ac.in,dc=bits-pilani,dc=ac,dc=in"
                oRoot.Username = "uid=" + username + ",ou=people,dc=bits-pilani,dc=ac,dc=in"
                oRoot.Password = password
                oSearcher.Filter = "(&(objectclass=*)(uid=" & username & "))"
                oResults = oSearcher.FindAll
                For Each oResult In oResults
                    foundUser = True
                    Dim test As String
                    test = ""
                    test = "Test Ok"
                    Response.Write(test)
                Next
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
            Return 
    Dim foundUser As Boolean
            'Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/ou=People,o=bits-pilani.ac.in,dc=bits-pilani,dc=ac,dc=in")
            Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & "/ou=People,dc=bits-pilani,dc=ac,dc=in")

            Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
            Dim oResults As SearchResultCollection
            Dim oResult As SearchResult

            Try

                foundUser = False
                oRoot.AuthenticationType = AuthenticationTypes.FastBind
                ' oRoot.Username = "uid=" + username + ",ou=people,o=bits-pilani.ac.in,dc=bits-pilani,dc=ac,dc=in"
                oRoot.Username = "uid=" + username + ",ou=people,dc=bits-pilani,dc=ac,dc=in"
                oRoot.Password = password

                oSearcher.Filter = "(&(objectclass=*)(uid=" & username & "))"
                oResults = oSearcher.FindAll

                For Each oResult In oResults
                    foundUser = True

                    Dim test As String
                    test = ""
                    test = "Test Ok"
                    Response.Write(test)
                Next

            Catch ex As Exception
                Response.Write(ex.Message)
            End Try

            Return foundUser




    Thank You 


    Atul Runthala

    atul.runthala@gmail.com

    Monday, March 8, 2010 7:47 AM