在asp中通过LDAP进行用户身份验证

时间:2011-10-30 18:24:40

标签: authentication asp-classic ldap

我想将用户名和密码传递给LDAP并检索用户信息。我有下面的代码,但不知道我必须在哪里添加密码字符串?

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)
.
..
...

1 个答案:

答案 0 :(得分:3)

这对我有用:

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