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 SkipVought 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
0
0
US
Could someone post an example of ASP code used to query LDAP without any proprietary components?

Thanks! Take Care,
Mike
 
Hey Z,

Anytime you get a chance is fine. Does seem like you got lots to do. I do appreciate the help though!

Thanks
CN
 
CoolNutz,
What code are you using to retrieve manager?
 
Z,

Im just adding the manager to the attributes it looks up on the user.

usrAttributes = "givenName,sn,manager"

CN
 
CoolNutz,
All done:

Code:
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 manager FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set rs = Com.Execute
manager=rs("manager")
rs.Close
managercn=(split((split(manager,",OU="))(0),"="))(1)
managercn=replace(managercn,"\","")
Com.CommandText="select sn,givenName FROM 'GC://"+objADsPath+"' where name='"+managercn+"'"
Set rs = Com.Execute
sn=rs("sn")
givenname=rs("givenName")
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
 
CoolNutz,
change:
Code:
managercn=(split((split(manager,",OU="))(0),"="))(1)
to:
Code:
managercn =Left((split(manager,"="))(1),(Len((split(manager,"="))(1))-3))
 
Z,

Once again you have come through! thanks a bunch!
I'll let you know how it goes.

CN
 
Z,

I tried to bring in your code into my code... guess i must have screwed up some where. I keep getting this error

Microsoft VBScript runtime error '800a01a8'
Object required: ''
/forms/formshome.asp, line 39

Here is my code....

user = Request.ServerVariables("Auth_User")
Set RootDSE = GetObject("LDAP://RootDSE")
DomainADsPath = RootDSE.Get("defaultNamingContext")
Set RootDSE = Nothing
Set Con = Server.CreateObject("ADODB.Connection")
Con.Provider = "ADsDSOObject"
Con.Open "ADProvider"
Set Cmd = Server.CreateObject("ADODB.Command")
Set Cmd.ActiveConnection = Con

usrAttributes = "givenName,sn,manager"

usrADOQuery = "<LDAP://" & DomainADsPath & ">;(&(objectCategory=person)(objectClass=*)(name=" & user & "));" + usrAttributes + ";subtree"
Cmd.CommandText = usrADOQuery

Set rs = Cmd.Execute
loginame = rs.Fields("givenName") & " " & rs.Fields("sn")
Session("loginame")=loginame
manager=rs("manager")
rs.Close

managercn =Left((split(manager,"="))(1),(Len((split(manager,"="))(1))-3))
managercn=replace(managercn,"\","")
--> Line 39 Com.CommandText="select sn,givenName FROM 'GC://"+objADsPath+"' where name='"+managercn+"'"
Set rs = Com.Execute
sn=rs("sn")
givenname=rs("givenName")

rs.Close
Con.Close
Set rs = Nothing
Set Con = Nothing

I know your busy, so when you get a chance, if you have any ideas, please let me know.

Thanks
 
fredb23,
I did reply to you with a question:

group membership and the ou that the user is in are two completely different things.
Are you looking to get one of these or both?
 
CoolNutz:

Done:

Code:
<%@ Language=VBScript %>
<%
Option Explicit
Dim user,RootDSE,DomainADsPath,Con,Cmd,usrAttributes,usrADOQuery,rs,loginame,manager,managercn,mgrAttributes,mgrADOQuery,mgrsn,mgrgivenname
%>
<html>
<head>
</head>
<body>
<%
user = Request.ServerVariables("Auth_User")
user = Right(user, Len(user) - InStrRev(user, "\"))
Set RootDSE = GetObject("LDAP://RootDSE")
DomainADsPath = RootDSE.Get("defaultNamingContext")
Set RootDSE = Nothing
Set Con = Server.CreateObject("ADODB.Connection")
Con.Provider = "ADsDSOObject"
Con.Open "ADProvider"
Set Cmd = Server.CreateObject("ADODB.Command")
Set Cmd.ActiveConnection = Con

usrAttributes = "givenName,sn,manager"

usrADOQuery = "<LDAP://" & DomainADsPath & ">;(&(objectCategory=person)(objectClass=*)(sAMAccountname=" & user & "));" + usrAttributes + ";subtree"

Cmd.CommandText = usrADOQuery

Set rs = Cmd.Execute
loginame = rs.Fields("givenName") + " " + rs.Fields("sn")
'Session("loginame")=loginame
manager=rs.Fields("manager")
rs.Close

managercn =Left((split(manager,"="))(1),(Len((split(manager,"="))(1))-3))
managercn=replace(managercn,"\","")

mgrAttributes = "givenName,sn"
mgrADOQuery = "<LDAP://" & DomainADsPath & ">;(&(objectCategory=person)(objectClass=*)(name=" & managercn & "));" + mgrAttributes + ";subtree"

Cmd.CommandText = mgrADOQuery

Set rs = Cmd.Execute
mgrsn=rs.Fields("sn")
mgrgivenname=rs.Fields("givenName")

rs.Close
Con.Close
Set rs = Nothing
Set Con = Nothing

response.write mgrsn&"<br>"
response.write mgrgivenname&"<br>"
%>
</body>
</html>
 
Z, again PERFECT! thanks. But just for knowledge or curiousity, what was the problem?

Thanks
CN
 
I saw a couple of problems:
The small section of script I gave you was written so that the queries are writen in a SQL format. Your queries are being done in a LDAP format. With that there are a few small differences when extracting records, etc.
Plus, in your usrADOQuery line i had to change
(name=" & user & ")
to
(sAMAccountname=" & user & ")
The user variable you have defined is not the LDAP field "name", it is "sAMAccountname"
It is possible the these two values are identical on your setup.
 
oops, sorry Zcolton, i did miss your reply ...

yes, its the OU the user is in i'm trying to retrieve.

thanks a lot
 
fredb23,
the field you want to retrieve is adspath

Code:
strSQL = "SELECT adspath FROM GC://"+domainName+"' WHERE objectCategory='person' AND sAMAccountname ='"+userName+"' "
 
The department fields do have something in them. It is only for the intranet, so I cannot get you to view it. The server is a member, not controller. Anonymous is still on by default, but Windows Integrated was checked as well. I already set that to Basic, so both are checked. Is it necessary for only one?
 
HisMightiness,
For testing purposes only use one (basic). Integrated must not be checked. Make those changes and let me know what happens. If it doesn't seem to work, there are a few tests we can do.
 
Nice one Zcolton ! thanks
Adspath, as the name says, return the full path of the AD user so i get something like :
LDAP://CN=some user name,OU=W2000 XP,OU=computers and users,OU=Informatique,OU=society,DC=some domain,DC=net

I can work with that string to extract the information i need.

Out of curiosity what would be the field for group membership ?

cheers
 
fredb23,
"memberOf" gives you an array of the groups that a user is in. Each item in the array is the full adspath of each group. The only group not listed is the Primary Group - (usually "Domain Users")
 
zcolton,

I tried your suggestion, and I am still getting the same result (no result). What would be the next step to try?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top