Active Directory AD authentication in Classic ASP

Function UserOkRichStyle(szUserName,szPassword) 'True if success; false otherwise

                

                Set oCommand = CreateObject("ADODB.Command")

                Set oConnection = CreateObject("ADODB.Connection")

    on error resume next

                ' Get the Configuration Naming Context.

                Set oRootDSE = GetObject("LDAP://RootDSE")

                varDomainNC = oRootDSE.Get("defaultNamingContext")

                ' Open the Connection

                oConnection.Provider = "ADsDSOObject"

                oConnection.Properties("User ID") = "YOUR_DOMAIN_HERE\" & szUserName

    oConnection.Properties("Password") = szPassword

                oConnection.Open "ADs Provider"

                ' Build the query to find the user based on their alias.

                strQuery = "<LDAP://" & varDomainNC & ">;(&(objectClass=user)(objectCategory=person)" & _

                           "(samAccountName=" & Trim(szUserName) & "));adspath;subtree"

                'response.write strquery

                oCommand.ActiveConnection = oConnection

                oCommand.CommandText = strQuery

                on error resume next

                Set RS = oCommand.Execute

    'response.write " RS.RecordCount : " & RS.RecordCount & "<BR>"

                If RS.RecordCount = 0 Then

                                UserOkRichStyle =false

                Else

                UserOkRichStyle = true

    end if

end function