Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to query Active Directory w/ asp

Status
Not open for further replies.

bslintx

Technical User
Apr 19, 2004
425
US
Hi all,

Having a heck of a time trying to find examples of how to query an Active Directory database. The following code works w/o a hiccup; however, I can't seem to figure out how to setup the strQuery like I am used to querying an Access/SQL database. I am trying to do a simple query to grab all users and get a count. This code simply grabs a record based on logon. So it will always be a single rec return. This is not the only query I'd like to do; however, if anyone knows how to convert to a sql string like we know how to use w/ Access or MSSQL then I'd be good to go. I'd like to add that those that ACTUALLY use a snippet that works to please post. I mention this because I had found a site w/ sql string but simply came up a bust from either of these two errors: 1. Unspecified error 2. type mismatch

While the first was basically a no-way-to-figure-out situation the second had my head scratching because I was looking for a string not a number and as far as I know it didn't involve an array to split. Please help! Thanks

Code:
<%
Function UserInfo(LoginName) 

Dim conn 
Dim rs 
Dim oRoot 
Dim oDomain 
Dim sBase 
Dim sFilter 
Dim sDomain 

Dim sAttribs 
Dim sDepth 
Dim sQuery 
Dim sAns 

Dim user 
 ' Logon contains various domain names - take out domain
   domain_logon_name = Request.ServerVariables("LOGON_USER")

   ' Need to get logon name only by using \ character 
   char_position = Instr(domain_logon_name, "\") 

domain_logon_name_length = Len(domain_logon_name)
LogonName = Right(domain_logon_name, domain_logon_name_length - char_position)
LogonName = "daffy.duck"
Set oRoot = GetObject("LDAP://rootDSE")
Set conn = Server.CreateObject("ADODB.Connection")
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
  & LoginName & "))"
sAttribs = "adsPath"
sDepth = "subTree"

sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
                   
conn.Open _
  "Data Source=Active Directory Provider;Provider=ADsDSOObject"
  
Set rs = conn.Execute(sQuery)

If Not rs.EOF Then
    Set user = GetObject(rs("adsPath"))
    With user   
    
    sAns = "First Name:(FirstName) " & .FirstName & "<br>"
    sAns = sAns & "Last Name:(LastName) " & .LastName & "<br>"
    sAns = sAns & "Office:(physicalDeliveryOfficeName) " & .physicalDeliveryOfficeName & "<br>"
    sAns = sAns & "Title:(title) " & .title & "<br>"
    sAns = sAns & "Initial:(initials) " & .initials & "<br>"
    sAns = sAns & "Department:(department) " & .department & "<br>"
    sAns = sAns & "Manager: " & .Manager & "<br>"
    sAns = sAns & "Phone Number:(TelephoneNumber) " & .TelephoneNumber & "<br>"    
    sAns = sAns & "Email Address:(EmailAddress) " & .EmailAddress & "<br>"
    sAns = sAns & "Description:(description) " & .description & "<br>"
    sAns = sAns & "Name:(name) " & .name & "<br>"
    sAns = sAns & "Street Address:(streetAddress) " & .streetAddress & "<br>"
    sAns = sAns & "Country:(co) " & .co & "<br>"
    sAns = sAns & "City:(l) " & .l & "<br>"
    sAns = sAns & "Zip Code:(postalCode) " & .postalCode & "<br>"
    sAns = sAns & "State:(st) " & .st & "<br>"
    sAns = sAns & "Email Address(mail): " & .mail & "<br>"
       response.write sAns
    End With
Else
response.Write "No user"
End If


Set oRoot = Nothing
Set oDomain = Nothing
End Function

LogonName = Request.ServerVariables("LOGON_USER")

Call UserInfo(LogonName)
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top