User authentication over LDAP in asp

15,508

This worked for me:

function AuthenticateUser(Username,Password,Domain)
        dim strUser,strPass,strQuery,oConn,cmd,oRS
        AuthenticateUser = false
        strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE objectClass='*'"
        set oConn = server.CreateObject("ADODB.Connection")
        oConn.Provider = "ADsDSOOBJECT"
        oConn.properties("User ID") = Username
        oConn.properties("Password")=Password
        oConn.properties("Encrypt Password") = true
        oConn.open "DS Query", Username,Password
        set cmd = server.CreateObject("ADODB.Command")
        set cmd.ActiveConnection = oConn
        cmd.CommandText = strQuery
        on error resume next
        set oRS = cmd.Execute
        if oRS.bof or oRS.eof then
            AuthenticateUser = false
        else
            AuthenticateUser = true
        end if
        set oRS = nothing
        set oConn = nothing
end function
Share:
15,508
uzay95
Author by

uzay95

Love this game (walking, talking as a programmer) :)

Updated on June 04, 2022

Comments

  • uzay95
    uzay95 almost 2 years

    I want to pass username and password to LDAP and retrieve user information. I have code below but don't know where must I add password string in it?

    strUsername = Request.Form("username")
    strPassword = Request.Form("password")
    
    Set rootDSE = GetObject("LDAP://RootDSE")
    Set oConn = CreateObject("ADODB.Connection")
    
    sDomainContainer = rootDSE.Get("defaultNamingContext")
    Debug "DomainContainer: " & sDomainContainer
    oConn.Properties("Encrypt Password") = true
    oConn.Provider = "ADSDSOObject"
    oConn.properties("user id") = sLdapReaderUsername
    oConn.properties("password") = sLdapReaderPassword
    
    oConn.Open "ADs Provider"
    
    sQuery = "<LDAP://" & sDomainContainer & ">;(sAMAccountName=" & strUsername & ");adspath,mail,displayName;subtree"
    
    Set userRS = oConn.Execute(sQuery)
    
    If Not userRS.EOF and not err then
        sFullName = userRS("displayName")
        sEmail = userRS("mail")
        sExternalID = ""
        sOrganization = ""
    
        Response.Write("sFullName: "&sFullName)
        Response.Write("sEmail: "&sEmail)
    .
    ..
    ...