By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,867 Members | 1,981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,867 IT Pros & Developers. It's quick & easy.

Active Directory Query in ASP

maxamis4
Expert 100+
P: 295
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ LANGUAGE=VBSCRIPT %>
  3. <%Option Explicit%>
  4.  
  5. <%
  6.     Function getADUserInfo(strUID)
  7.         Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strLastLogin, strSid, strBase        
  8.         'on error resume next
  9.         strGeneralLookupError = false
  10. strUID = "UserName"
  11.  
This is the statement that returns an empty value.
Expand|Select|Wrap|Line Numbers
  1. strBase = "<LDAP://DC=subdomain,DC=subdomain,DC=primarydomain,DC=masterdomain>
Expand|Select|Wrap|Line Numbers
  1.         response.write strbase
  2.         strFilter = "(&(objectCategory=person)(objectClass=user)(SAMAccountName=*" & strUID & "))"        
  3.         strAttributes = "cn, mail, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
  4.         'strAttributes = "cn, company, givenName, sn, ADsPath, name, sAMAccountName, telephoneNumber"
  5.         strScope = "subtree"    
  6.         strFullCommand = strBase & ";" & strFilter & ";" & strAttributes & ";" & strScope
  7.  
  8.  
  9.     response.write strfullcommand
  10.  
  11.         set rsADUserInfo = Server.CreateObject("ADODB.Recordset")
  12.         set    rsADUserInfo = connAD.Execute(strFullCommand)
  13.         if err.number <> 0 then
  14.             strGeneralLookupError = true
  15.         end if
  16.         set getADUserInfo = rsADUserInfo
  17.         set rsADUserInfo = Nothing
  18.     End Function
  19.  
  20.     Sub getUserData(p_strUserID)
  21.         'on error resume next
  22.         set rsUserData = Server.CreateObject("ADODB.Recordset")
  23.         set rsUserData = getADUserInfo(p_strUserID)
  24.         if not rsUserData.EOF then
  25.             strUserGN = rsUserData("givenName")
  26.             strUserSN = rsUserData("sn")
  27.             strUserOU = rsUserData("company")
  28.             strUserEmail = rsUserData("mail")
  29.             strUserPhone = rsUserData("telephoneNumber")
  30.         else
  31.             strADLookupSuccess = false
  32.         end if
  33.         rsUserData.Close
  34.         set rsUserData = Nothing
  35.     End Sub
  36.  
  37.     'on error resume next
  38.  
  39.     response.expires = 0
  40.  
  41.     DIM connAD, rsUserData, rsADUserInfo
  42.     DIM strUserGN, strUserSN, strUserOU, strUserEmail, strUserPhone
  43.     DIM strBase, strFilter,strAttributes, strScope, strFullCommand
  44.     DIM strGeneralLookupError, strADLookupSuccess
  45.     DIM strUserID
  46.  
  47.     strUserGN = "The user can not be found in the system."
  48.     strGeneralLookupError = false
  49.     strADLookupSuccess = true
  50.  
  51.     set connAD = Server.CreateObject("ADODB.Connection")
  52.     connAD.Provider = "ADsDSOObject"
  53.     connAD.Open "Active Directory Provider"
  54.  
  55.     strUserID = "JuanPGomez"
  56.     call getUserData(strUserID)
  57.  
  58.     connAD.Close
  59.     set connAD = Nothing
  60. %>
  61.  
  62. <html>
  63. <head>
  64.     <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  65.     <title>ASP Code to access AD with LDAP Page</title>
  66. </head>
  67.     <body>
  68.         <%=strUserGN%>&nbsp;
  69.         <%=strUserSN%><br />
  70.         <%=strUserOU%><br />
  71.         <%=strUserEmail%><br />
  72.         <%=strUserPhone%><br />
  73.     </body>
  74. </html>
  75.  
Oct 9 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.