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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Example of ASP LDAP query string? 14

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
Could someone post an example of ASP code used to query LDAP without any proprietary components?

Thanks! Take Care,
Mike
 
CityWebGuy,
The easiest way would be to cut off the portions you don't want to use. Rip off the 'CN=' and then split the string by using the commas. You first value in the array is the name only. If you would like I can put the code together for you.

Savvy95,
Please post the modified code so that I can take a look at it.

zcolton
 
zcolton,

Thanks for thoses changes IT all works great. I am also trying to do somthing simular to CityWebGuy, using your earlyer posted code, so if you could show us how to change that so it only shows the group names it would be great( code attached bellow.

also would it also be easy to add a button that would give us a list of groups in the system in the list box and show us the group membership in the right? (ie. same as user + deatil)

thanks one again for all your work

Scott

<%@ Language=VBScript %>
<%
Option Explicit
Dim strUsername,con,rs,Com,objADsPath,objDomain,membership,group
%>
<html>
<head>
</head>
<body>
<%
strUsername = request.queryString("user")
strUserName = Right(strUserName, Len(strUserName) - InStrRev(strUserName, "\"))
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = Server.CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select memberof FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rs = Com.Execute
membership=rs("memberof")
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
For each group in membership
response.write group&"<br>"
Next
%>

</body>
</html>
 
Here's the script that doesn't work. When I use it a blank (white) page appears with nothing on it. If I use the original without "description" I get name telephone and email. All your help is appreciated


<%@ Language=VBScript %>
<%
fsCompletted = Request.QueryString("f")

If fsCompletted = 1 Then
ResultHTML = ProcessForm
else
ResultHTML = "<br>"
End If

Function ProcessForm
on error resume next
oUsername=Request.Form("username")
oPassword=Request.Form("password")
strUserName = Right(oUsername, Len(oUsername) - InStrRev(oUsername, "\"))
if InStr(strUserName, "@")>0 then
strUserName = Left(strUserName, InStr(strUserName, "@")-1)
end if
Set objDomain = GetObject("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = Server.CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.Properties("User ID") = oUsername
con.Properties("Password") = oPassword
con.Properties("Encrypt Password") = False
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select name,telephonenumber,mail,description FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rs = Com.Execute
if err.number=0 then
HTML = "<p>"+rs("name")+"<br>"+rs("telephonenumber")+"<br>"+rs("mail")+"<br>"+rs("description")+"</p>"
else
HTML = "<p>Not Authenticated</p>"
end if
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
ProcessForm=HTML
End Function

PostURL = Request.ServerVariables("SCRIPT_NAME") & "?f=1"

%>
<HTML>
<HEAD>
</HEAD>
<BODY>
<%=ResultHTML%>
<%
if request.querystring("f")="" then
%>

<form method="post" Action="<%=PostURL%>">
<p>Username : <input type="text" Name="username" Size=50></p>
<p>Password : <input type="password" Name="password" Size=10></p>
<input Name=SubmitButton Value="Authenticate User" Type=submit>
</form>

<%end if%>

</BODY>
</HTML>
 
To all of those having problems retrieving the value of "description" when grabbed from user details:

Though it may seem that the feild should just be a simple string it is actually an array. After creating a recordset including description, set a variable equal to the field and use the index 0 to display the value. Example:
(This is not complete code, only an example or usage.)

Code:
Com.CommandText ="select description FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rs = Com.Execute
descriptionarray=rs("description")
descriptionstring=description(0)

zcolton
 
plasma2,

To display group names:

Code:
For each group in membership
 newgroup=split(group,"=")
 response.write left(newgroup(1), len(newgroup(1))-3)&"<br>"
Next

zcolton
 
zcolton,

Thanks for the reply. I was able to split the string into the array and search that array.

I figured I would have to do something like that I just wanted to make sure there was no other way to just pull the name straight out with a LDAP query.

Thanks

 
I'm sorry guys for bothering you, but I i'd like some help. Where do I put your wonderful code into the asp page?
 
savvy95
Which code and which page? Also, here is an asp page that will list all groups in AD. Each will be a hyperlink. Click the link and it will list all users that are members of that group:

Code:
<%@ Language=VBScript %>
<%
response.buffer = true
SUB CloseAll
   rs.close
   set rs=nothing
   con.close
   set con=nothing
END SUB
%>
<html>
<head>
</head>
<body>
<%
groupdsn=request.querystring("group")
if groupdsn="" then
 Set objDomain = GetObject ("GC://rootDSE")
 objADsPath = objDomain.Get("defaultNamingContext")
 Set objDomain = Nothing
 Set con = Server.CreateObject("ADODB.Connection")
 con.provider ="ADsDSOObject"
 con.open "Active Directory Provider"
 Set Com = CreateObject("ADODB.Command")
 Set Com.ActiveConnection = con
 Com.CommandText ="select adspath,name from 'GC://"+objADsPath+"' WHERE objectCategory='Group' ORDER BY name"
 Set rs = Com.Execute
 if rs.EOF then
  Call CloseAll
  response.write "No Groups Found."
  else
   rsarray=rs.getrows
   Call CloseAll
   numrows=ubound(rsarray,2)
   for rowcounter=0 to numrows
    response.write "<a href='"+Request.ServerVariables("SCRIPT_NAME")+"?group="+rsarray(1,rowcounter)+"'>"+rsarray(0,rowcounter)+"</a><br>"
   next
 end if
 else
 set objgroup=GetObject(groupdsn)
 response.write "<b>Members of "+groupdsn+"</b><br>"
 For each objMember in objGroup.Members
  response.write objMember.cn+"<br>"
 Next
 Set objGroup=nothing
end if
%>
</body>
</html>

zcolton
 
Thank you again, BUT.. it doesn't work for me. I get

Error Type:
Provider (0x80040E37)
Table does not exist.
/UserPortal/groups.asp, line 27

What in the world am I doing wrong?


 
savvy95

That seems to me like you are having permission issues accessing the information. That is the Error I got when I did not have my permission set correctly to access the
GC.

 
OK. That might be it. I'll take a look. I'm in the Domain Admins group though.
 
savvy95,
My last asp page I posted does need to run under the context of a domain user. If you are running these on your web server, be sure that you are not accessing it under the user context of the IUSR_ anonymous account.

zcolton
 
Zcolton,

I am trying to use the otherTelephone field on my page but i keep getting the following error

"Response object error 'ASP 0106 : 80020005'
Type Mismatch
/phone/info.asp, line 0
An unhandled data type was encountered. "

<%@ Language=VBScript %>
<%
Option Explicit
response.buffer = true
Dim con,rs,Com,user,objADsPath,objDomain,objADOU,objRootOU,objothertelephone,newothertelephone
%>
<html>
<head>
<style type="text/css" media="screen, tv, projection"> @import "phonebook.css"; </style>
</head>
<body bgcolor="#DFDFDF" topmargin="0">
<%
user = request.queryString("user")
If user <> "" Then
objRootOU="ou=staff,"
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = Server.CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select otherTelephone, name, department, title, mail, telephonenumber, mobile, facsimileTelephoneNumber, physicalDeliveryOfficeName, sAMAccountname, company from 'GC://"+objRootOU+objADsPath+"' where sAMAccountname='"+user+"'"
Set rs = Com.Execute

%>
<table border="0" cellpadding="0" bgcolor="#DFDFDF">
<tr><td id="userinfo" colspan="2"><b><% response.write rs("name")%></b> - <% response.write rs("company")%></td></tr>
<tr><td id="userinfo">Title: </td><td><% response.write rs("title")%></tr>
<tr><td id="userinfo">Branch: </td><td><% response.write rs("department")%></tr>
<tr><td id="userinfo">Office: </td><td><% response.write rs("physicalDeliveryOfficeName")%></tr>
<tr><td id="userinfo">E-mail Address: </td><td><A href="mailto:<% response.write rs("mail")%>"><% response.write rs("mail")%></td></tr>
<tr><td id="userinfo">Phone Extension: </td><td><% response.write rs("telephonenumber")%>, <% response.write rs("otherTelephone")%></tr>
<tr><td id="userinfo">Mobile Number: </td><td><% response.write rs("mobile")%></tr>
<tr><td id="userinfo">Fax Number: </td><td><% response.write rs("facsimileTelephoneNumber")%></tr>

</table>

<%
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
Else%>
<p><br>Click on the name<br>of the person for<br>contact info.</p>
<%End If%>
</body>
</html>



Thanks Heaps
 
Plasma2,

otherTelephone is an array

Code:
<%@ Language=VBScript %>
<%
response.buffer = true
%>
<html>
<head>
<style type="text/css" media="screen, tv, projection"> @import "phonebook.css"; </style>
</head>
<body bgcolor="#DFDFDF" topmargin="0">
<%
user = request.queryString("user")
If user <> "" Then
objRootOU="ou=staff,"
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set con = Server.CreateObject("ADODB.Connection")
con.provider ="ADsDSOObject"
con.open "Active Directory Provider"
Set Com = CreateObject("ADODB.Command")
Set Com.ActiveConnection = con
Com.CommandText ="select otherTelephone, name, department, title, mail, [URL unfurl="true"]wwwhomepage,[/URL] telephonenumber, mobile, facsimileTelephoneNumber, physicalDeliveryOfficeName, company from 'GC://"+objRootOU+objADsPath+"' where sAMAccountname='"+user+"'"
Set rs = Com.Execute
otherTelephone=rs("otherTelephone")
name=rs("name")
department=rs("department")
title=rs("title")
mail=rs("mail")
[URL unfurl="true"]wwwhomepage=rs("wwwhomepage")[/URL]
telephonenumber=rs("telephonenumber")
mobile=rs("mobile")
facsimileTelephoneNumber=rs("facsimileTelephoneNumber")
physicalDeliveryOfficeName=rs("physicalDeliveryOfficeName")
company=rs("company")
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
if isarray(otherTelephone) then
 for each strnumber in otherTelephone
  strotherTelephone=","&strotherTelephone&","&strnumber
 next
end if
%>
<table border="0" cellpadding="0" bgcolor="#DFDFDF">
<tr><td id="userinfo" colspan="2"><b><%=name%></b> - <%=company%></td></tr>
<tr><td id="userinfo">Title: </td><td><%=title%></tr>
<tr><td id="userinfo">Branch: </td><td><%=department%></tr>
<tr><td id="userinfo">Office: </td><td><%=physicalDeliveryOfficeName%></tr>
<tr><td id="userinfo">E-mail Address: </td><td><a href="mailto:<%=mail%>"><%=mail%></a></td></tr>
<tr><td id="userinfo">Phone Extension: </td><td><%=telephonenumber%><%=strotherTelephone%></td></tr>
<tr><td id="userinfo">Mobile Number: </td><td><%=mobile%></tr>
<tr><td id="userinfo">Fax Number: </td><td><%=facsimileTelephoneNumber%></tr>
</table>
<%Else%>
<p><br>Click on the name<br>of the person for<br>contact info.</p>
<%End If%>
</body>
</html>
 
I am looking at doing something similar to your phone book. I would like to allow my users to search for as specific record either by name or by department using a web form. Is this possible?
 
valdeloire

Anything is possible. Give until early next week, and I can have an example ready for you.

zcolton
 
Hi!

The code, what i find here, doesn't work, except on my computer (IIS is on my computer):

1 set ADSysInfo = CreateObject("ADSystemInfo")
2 response.write ADSysInfo.UserName
3 set CurrentUser = GetObject("LDAP://" & ADSysInfo.UserName)
4 if IsArray(CurrentUser.MemberOf) then
5 strGroups = LCase(Join(CurrentUser.MemberOf))
6 else
7 strGroups = LCase(CurrentUser.MemberOf)
8 end if
9 response.write strGroups
10 set ADSysInfo = nothing
11 set CurrentUser = nothing
12
13 if instr(strGroups, "cn=cst") then
14 cstcheck=1
15 end if

error on line 3
(0x800050000)

Do anyone know what settings may wrong with my IIS or AD?
Thank in advance!
 
molnara (and to anyone else that may read this),

The number 1 item that most people over look is the security context in which you run these scripts. For the majority of the scripts that I have written, they must run under the context of a domain user. The anonymous IIS account is NOT a domain user. If you do not disable anonymous access or switch the anonymous account that IIS uses to a domain account, you will always get an access denied error (or in the case of LDAP queries - table not found). So, molnara, under what security context are you running the script?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top