Active Directory Query in ASP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxamis4
    Recognized Expert Contributor
    • Jan 2007
    • 295

    Active Directory Query in ASP

    Folks

    I have found a code that I am trying to use to query active directory. The code executes without a problem so I am assuming the syntax works. My problems is that a variable I am using does not retain its value. In the AD query it shows up blank, which really puzzles me. See the code I have created below from an article I found that was posted.

    Code:
    <%@ LANGUAGE=VBSCRIPT %>
    <%Option Explicit%>
    
    <%
    	Function getADUserInfo(strUID)
    		Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strLastLogin, strSid, strBase		
    		'on error resume next
    		strGeneralLookupError = false
    strUID = "UserName"
    This is the statement that returns an empty value.
    Code:
    strBase = "<LDAP://DC=subdomain,DC=subdomain,DC=primarydomain,DC=masterdomain>
    Code:
    		response.write strbase
    		strFilter = "(&(objectCategory=person)(objectClass=user)(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
    	
    
    	response.write strfullcommand
    		
    		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.Open "Active Directory Provider"
    	
    	strUserID = "JuanPGomez"
    	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>
Working...