Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: ASP code to query AD with LDAP

Results 1 to 4 of 4
Share This Thread →
  1. #1
    markoc's Avatar
    markoc is offline Contributing User
    Join Date
    Nov 2003
    Location
    UK
    Posts
    2,280
    Rep Power
    310

    ASP code to query AD with LDAP

    the following code let's you query a domain AD to get users details.

    I've split the query into two parts, so if you want to get more than one users details and want different info for each user you can add another sub and get what info you want.

    Code:
    <%@ LANGUAGE=VBSCRIPT %>
    <%Option Explicit%>
    
    <%
    Function getADUserInfo(strUID)
    	on error resume next
    	strGeneralLookupError = false
    	strBase = "<LDAP://DC=[DOMAIN], DC=[DOMAIN EXETENTION]>"
    	strFilter = "(sAMAccountName=" & strUID & ")" 
    	strAttributes = "cn, mail, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
    	'strAttributes = "cn, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
    	strScope = "subtree"	
    	strFullCommand = strBase & ";" & strFilter & ";" & strAttributes & ";" & strScope
    	set rsADUserInfo = Server.CreateObject("ADODB.Recordset")
    	set	rsADUserInfo = connAD.Execute(strFullCommand)
    	if err.number <> 0 then
    		strGeneralLookupError = true
    	end if
    	set getADUserInfo = rsADUserInfo
    	set rsADUserInfo = Nothing
    End Function
    
    Sub getUserData(p_strUserID)
    	on error resume next
    	set rsUserData = Server.CreateObject("ADODB.Recordset")
    	set rsUserData = getADUserInfo(p_strUserID)
    	if not rsUserData.EOF then
    		strUserGN = rsUserData("givenName")
    		strUserSN = rsUserData("sn")
    		strUserOU = rsUserData("company")
    		strUserEmail = rsUserData("mail")
    		strUserPhone = rsUserData("telephoneNumber")
    	else
    		strADLookupSuccess = false
    	end if
    	rsUserData.Close
    	set rsUserData = Nothing
    End Sub
    
    on error resume next
    
    response.expires = 0
    
    DIM connAD, rsUserData, rsADUserInfo
    DIM strUserGN, strUserSN, strUserOU, strUserEmail, strUserPhone
    DIM strBase, strFilter,strAttributes, strScope, strFullCommand
    DIM strGeneralLookupError, strADLookupSuccess
    DIM strUserID
    
    strUserGN = "The user can not be found in the system."
    strGeneralLookupError = false
    strADLookupSuccess = true
    
    set connAD = Server.CreateObject("ADODB.Connection")
    connAD.Provider = "ADsDSOObject"
    connAD.Properties("User ID") = "[DOMAIN]\[USERNAME]" ' ### remember to make sure this user has rights to access AD
    connAD.Properties("Password") = "[PASSWORD]"
    connAD.Properties("Encrypt Password") = true
    connAD.Open
    
    strUserID = "[USERNAME YOU WANT INFO FOR]"
    call getUserData(strUserID)
    
    connAD.Close
    set connAD = Nothing
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>ASP Code to access AD with LDAP Page</title>
    </head>
    <body>
    <%=strUserGN%>&nbsp;
    <%=strUserSN%><br />
    <%=strUserOU%><br />
    <%=strUserEmail%><br />
    <%=strUserPhone%><br />
    </body>
    </html>
    Hope this advise helps.

    If so please show your appreciation by adding reputation points (click gauge image on top right of this post and score).

  2. #2
    shirley1 is offline Registered User
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    asp ldap IIS6 WINDOWS 2003

    I found this old article , i want to know if it works, since I have to validate the access to a html application and I want to use asp.

  3. #3
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    Quote Originally Posted by shirley1
    I found this old article , i want to know if it works, since I have to validate the access to a html application and I want to use asp.
    If it's posted here it mean it works, yes.
    Try using it and if you bump into errors/problems feel free to ask for help.

  4. #4
    jamjam200 is offline Registered User
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0
    Hi

    Thanks for this example works first time its great, was trying for ages before i found it to make some thing work.

    I added a search form to search for users based on surname,

    is it possible to return all results that match or partial match the surname, like a loop?

Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. PIN Code Login Application
    By xcelmind in forum ASP Development
    Replies: 1
    Last Post: January 8th, 2007, 07:27 AM
  2. Query LDAP Data Using ASP
    By cccplanner in forum ASP Development
    Replies: 2
    Last Post: October 11th, 2005, 05:47 PM
  3. LDAP query for Active Directory from a remote machine
    By Celine Cheung in forum ASP Development
    Replies: 3
    Last Post: November 12th, 2004, 11:26 AM
  4. ASP code to access query in MS Access
    By kpsga in forum ASP Free Lounge
    Replies: 2
    Last Post: September 23rd, 2003, 11:23 AM

ASP Free Advertisers and Affiliates