Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 4th, 2007, 11:33 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default LDAP bind to AD - get simple phone list

AD and LDAP is very new to me. I have been looking round, running examples and reading up on the topic. My objective is to create a phone list and provide the tools for users to update. I kept getting 'table not found' errors. My reaseach tells me this is an incorrct path. I then went down the path of getting conformation of connectivity by way of authentication, this was achieved using:

<%
     function AuthenticateUser(UserName, Password, Domain)
        dim strUser
        ' assume failure
        AuthenticateUser = false
        strUser = "\" & UserName
        strPassword = Password
        strQuery = "SELECT cn FROM 'LDAP://" & Domain & "' WHERE objectClass='*' "
        set oConn = server.CreateObject("ADODB.Connection")
        oConn.Provider = "ADsDSOOBJECT"
        oConn.Properties("User ID") = strUser
        oConn.Properties("Password") = strPassword
        oConn.Properties("Encrypt Password") = true
        oConn.open "DS Query", strUser, strPassword
        set cmd = server.CreateObject("ADODB.Command")
        set cmd.ActiveConnection = oConn
        cmd.CommandText = strQuery
        on error resume next
        set oRS = cmd.Execute
        if oRS.bof or oRS.eof then
            AuthenticateUser = false
        else
            AuthenticateUser = true
        end if
        set oRS = nothing
        set oConn = nothing
     end function

     dim submit,UserName,Password
        UserName = "JohnDoe"
        Password = ""
        Domain = "subdomain.domain.com"
        submit = request.form("submit")
        if submit = "Authenticate" then
           UserName = request.form("UserName")
           Password = request.form("Password")
           Domain = request.form("Domain")
           result = AuthenticateUser(UserName, Password, Domain)
           if result then
              response.write "<h3>Authentication Succeeded!</h3>"
              response.write result & "<br>"
              response.write strUser
           else
              response.write "<h3>Authentication Failed!</h3>"
           end if
        end if %>
<html>
<title>LDAP Authentication Test</title>
</head>
<body>
<form method=post>
<table>
 <tr>
  <td><b>Username:</b></tD>
  <td><input type="text" name="UserName" value="<% if request.form("userName") <> "" then response.write request.form("userName") end if %>" size="30"></td>
 </tr>
 <tr>
  <td><b>Password:&nbsp;</b></td>
  <td><input type="password" name="Password" value="<% if request.form("password") <> "" then response.write request.form("password") end if %>" size="30"></td>
 </tr>
 <tr>
  <td><b>AD Domain(IP):&nbsp;</b></tD>
  <td><input type="text" name="Domain" value="<% if request.form("domain") <> "" then response.write request.form("domain") end if %>" size="30"></td>
 </tr>
 <tr>
  <td colspan="2"><input name="submit" type="submit" value="Authenticate"></td>
 </tr>
</table>
</form>
</body>
</html>

I have been trial and error ing for the last few hours. We are running AD on win 2003. Can Anyone assist?

TYIA

Wind is your friend
Matt
__________________
Wind is your friend
Matt
 
Old February 5th, 2007, 10:17 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Some progress:

The following connects, authenticates and gets 'a users details' NOTE: no code changes are neccessary to get this working, it is cut n paste code (you need to place domain\userName in the username box)

Appart from there being no loop in place (I realize this would have to be done) why can I not change:
Com.CommandText ="select name,telephonenumber,mail FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"

To:
Com.CommandText ="select name,telephonenumber,mail FROM 'GC://"+objADsPath+"'"
To get all user details. When I do this, no error occurs However no details are rendered on the page. Any ideas?
-----------------------------start cut n paste code---------------------------
<%
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 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")+"</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" value="" Size="25">(yourDomain/username)</p>
     <p>Password : <input type="password" Name="password" Size="25"></p>
     <input Name=SubmitButton Value="Authenticate User" Type=submit>
     </form>
<%end if%>
</BODY>
</HTML>
---------------------------finish---------------------------------------------

Any assistance would be appreciated - TYIA

Wind is your friend
Matt
 
Old February 23rd, 2007, 10:26 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this..

domain = "whatever.com"
   user = "admin"
   password = "password12"

   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Provider = "ADsDSOObject"
   conn.Open "ADProvider", user, password
   filter = "(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(!(home MDB=*))(!(msExchHomeServerName=*)))(&(objectCatego ry=person)(objectClass=user)(msExchHomeServerName= *))(&(objectCategory=person)(objectClass=contact)) (objectCategory=group)(objectCategory=publicFolder ) ))))"

   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Provider = "ADsDSOObject"
   conn.Open "ADProvider", user, password

   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = conn
   cmd.CommandText = "<LDAP://" & domain & ">;" & filter & ";sn,givenName,telephoneNumber,ipphone,physicalDel iveryOfficeName,mail,title,pager,FacsimileTelephon eNumber,mobile,wWWHomePage;subtree"

   Set rs = cmd.Execute

%>
   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = conn
   cmd.CommandText = "<LDAP://" & domain & ">;" & filter & ";givenName,sn,telephoneNumber,ipphone,physicalDel iveryOfficeName,mail,title,pager,FacsimileTelephon eNumber,mobile,wWWHomePage;subtree"

   Set rs = cmd.Execute
While Not rs.EOF
if rs.Fields("physicalDeliveryOfficeName") = loc_name then 'I use to filter items only by Office Location that I pass from another page.

response.write("<td bgcolor=FFFFFF NOWRAP>" & rs.Fields("givenName") & "&nbsp;" & rs.Fields("sn") & "&nbsp;</td>")&vbcrlf
      response.write("<td bgcolor=FFFFFF NOWRAP>" & rs.Fields("ipphone") & "&nbsp;</td>")&vbcrlf
      response.write("<td bgcolor=FFFFFF NOWRAP>" & rs.Fields("telephoneNumber") & "&nbsp;</td>")&vbcrlf
      response.write("<td bgcolor=FFFFFF NOWRAP>" & rs.Fields("physicalDeliveryOfficeName") & "&nbsp;</td>")&vbcrlf
      response.Write("<td bgcolor=FFFFFF NOWRAP><a href=""mailto:" & rs.Fields("mail") & """>" & rs.Fields("mail") & "</a>&nbsp;</td>")&vbcrlf
else
      end if

      rs.MoveNext

  Wend

Good luck!
 
Old February 26th, 2007, 02:21 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Its the end of the day here, a very busy one...

Thank you for your post I will look into it first thing in the morning.

Wind is your friend
Matt
 
Old March 1st, 2007, 01:04 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

tball - thank you for your code. Your use of the word Filter was problematic for me. Are you are using this purely as a variable? I suspect so, this is a word used for a function in VBScript. Is it working for you? Anyhow I got your code working by changing this however I had to place domainName\username as the value in the user variable. Did your work with just user name?. Cheers mate, nice code.

For others here is another version. This version is my original with the bugs ironed out. It requires you to place domainName\username in the text box.

NOTE: tballs version has user credentials hard coded in the page. The following version uses a web form, its cut n paste, no alterations are neccesary:

--------------cut n past code----------------------

<%
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
   'works for a user
   'Com.CommandText ="select name,telephonenumber,mail FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
   'fails wip:
   'Com.CommandText ="select name,telephonenumber,mail FROM 'GC://"+objADsPath+"' where objectClass='"+user+"'"
   'fails wip:
   'Com.CommandText ="select name,telephonenumber,mail FROM 'GC://"+objADsPath+"' where sAMAccountname LIKE '*'"
   'gets all users
   Com.CommandText = "<GC://"+objADsPath+">;(&(objectCategory=person)(objectCl ass=user));displayName,mailNickname,sAMAccountName ,telephoneNumber,mail,name;subtree"
   Set rs = Com.Execute %>
<HTML>
 <HEAD>
 </HEAD>
<BODY>
<% if not rs.eof then %>
      <table>
       <tr>
        <td>Display Name</td>
        <td>Phone</td>
       </tr>
<% do until rs.eof %>
          <tr>
           <td><%= rs(1) %></td>
           <td><%= rs(3) %></td>
          </tR>
<% rs.moveNext
       loop %>
       </table>
<% end if
   rs.Close
   con.Close
   Set rs = Nothing
   Set con = Nothing
   ProcessForm=HTML
End Function
PostURL = Request.ServerVariables("SCRIPT_NAME") & "?f=1" %>
<%=ResultHTML%>
<%if request.querystring("f")="" then %>
     <form method="post" Action="<%=PostURL%>">
      <p>Username : <input type="text" Name="username" value="" Size="25">(yourDomain\username)</p>
      <p>Password : <input type="password" Name="password" Size="25"></p>
      <input Name=SubmitButton Value="Authenticate User" Type=submit>
     </form>
<%end if%>
</BODY>
</HTML>



-----------------finish----------------------------

Wind is your friend
Matt
 
Old June 10th, 2007, 02:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default

with the reference of your previous response.
above first code is working. when I enter correct domain/username and
pw it retrieve message that authentication success, otherwise wrong
password it return faile authentication message.

would you like to help us how can retreived the user first name and last name
or full name of logon user from the domain server ?

Thanks
Mateen









 
Old June 12th, 2007, 07:02 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

This will meet your needs however not cut n paste code, it requires the first three variables to be valid strings. You will see all sorts of information when you run it. Remove the values you do not wish to see and whaaalaa

<%
   domain = "xx.xx.xx.xx"
   user = "yourDomain\userName"
   password = "usersPassword"

   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Provider = "ADsDSOObject"
   conn.Open "ADProvider", user, password
   filterTTTT = "(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(!(home MDB=*))(!(msExchHomeServerName=*)))(&(objectCatego ry=person)(objectClass=user)(msExchHomeServerName= *))(&(objectCategory=person)(objectClass=contact)) (objectCategory=group)(objectCategory=publicFolder ) ))))"

   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Provider = "ADsDSOObject"
   conn.Open "ADProvider", user, password

   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = conn
   cmd.CommandText = "<LDAP://" & domain & ">;" & filterTTTT & ";sn,givenName,telephoneNumber,ipphone,physicalDel iveryOfficeName,mail,title,pager,FacsimileTelephon eNumber,mobile,wWWHomePage;subtree"

   Set rs = cmd.Execute


   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = conn
   cmd.CommandText = "<LDAP://" & domain & ">;" & filterTTTT & ";givenName,sn,telephoneNumber,ipphone,physicalDel iveryOfficeName,mail,title,pager,FacsimileTelephon eNumber,mobile,wWWHomePage;subtree"

   Set getInfo = cmd.Execute %>
   <table border="1">
<% do until getInfo.Eof %>
       <tr>
        <td NOWRAP><% if trim(getInfo(0)) <> "" then response.write trim(getInfo(0)) else response.write "&nbsp;" end if %></td>
        <td NOWRAP><% if trim(getInfo(1)) <> "" then response.write trim(getInfo(1)) else response.write "&nbsp;" end if %></td>
        <td NOWRAP><% if trim(getInfo(2)) <> "" then response.write trim(getInfo(2)) else response.write "&nbsp;" end if %></td>
        <td NOWRAP><% if trim(getInfo(3)) <> "" then response.write trim(getInfo(3)) else response.write "&nbsp;" end if %></td>
        <td NOWRAP><% if trim(getInfo(4)) <> "" then response.write trim(getInfo(4)) else response.write "&nbsp;" end if %></td>
        <td NOWRAP><% if trim(getInfo(5)) <> "" then response.write trim(getInfo(5)) else response.write "&nbsp;" end if %></td>
       </tr>
<% getInfo.MoveNext
   loop%>
   </table>

Wind is your friend
Matt
 
Old June 13th, 2007, 08:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for support.

I try above code, I give first three variables,
domain,user, and password
it give permission error

Error Type:
Provider (0x80040E09)
Permission denied.
/dcil/logon.asp, line 20

...
Set rs = cmd.Execute // line 20
....

why it give permssion error ?
users have read access to domain, user have member of domain.
users want to see his first,last or full name from domain.
how can first,last or full name can be retreive ?

2. it is necessary to give first three variable ?
   it could not be retrieve first,last or full name
   without give password ?

3. to retrieve the user password, it is another topic, to
   write separate coding to get user password ?

thnaks and regards
Mateen







 
Old June 13th, 2007, 06:30 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

;;;why it give permssion error
Becayse the computer you are running it from is not part of the domain or the user details are incorrect or do not have permissions for what you are trying to do

;;;how can first,last or full name can be retreive ?
by running the code, it works providing the user details you gave are correct. Post ALL the code you changed, this should only be the following three lines:

   domain = "xx.xx.xx.xx"
   user = "yourDomain\userName"
   password = "usersPassword"

;;it is necessary to give first three variable ?
of course it is, why else would I suggest it. You AD set up requires this.

;;;it could not be retrieve first,last or full name without give password ?
not only do you need password. You need Domain, user name and password - just like I said, and then said again...

;;to retrieve the user password, it is another topic, to write separate coding to get user password ?
You should get the user details before you want passwords. You need an admin account to get passwords, do you really think all you users want you knowing thier passwords????

The code I gave you works. I and other people I know use variations of it. make sure you have correct correct syntax and case for the three variables you need to add. As I said post ALL the code you chnaged (not the whole page)

Run this code to get your domain syntax, let me know the exact output:

<%@ Language=VBScript %>
<%
Option Explicit
Dim objADsPath,objDomain
%>
<html>
<head>
</head>
<body>
<%
Set objDomain = GetObject ("LDAP://RootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Response.Write objADsPath & "<BR>"
%>
</body>
</html>


Wind is your friend
Matt
 
Old June 14th, 2007, 12:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your response.

I run this code, output is following

DC=Parsons,DC=com


first three variable I give like this

domain = "domainname"
user = "p0012134"
password = "mmpsss125"

it give permission error

when I give without quotation like this

domain = domainname
user = p0012134
password = mmpsss125

this give following error

Error Type:
Active Directory (0x80040E37)
An invalid Active Directory pathname was passed
/dcil/logon.asp, line 20


Thanks and regards
Mateen





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add existing AD user to AD group via ADSI? thiazi Classic ASP Basics 0 August 24th, 2007 09:27 PM
Data Bind ComboBox to display the List Bjay Pro VB Databases 0 July 29th, 2007 02:57 PM
Simple LDAP Query to AD (without password) jwadew .NET Framework 2.0 1 March 1st, 2007 12:22 PM
can we bind datagrid and dropdown list rakeshgv SQL Server 2000 0 August 21st, 2006 02:13 AM
LDAP bmains ASP.NET 1.0 and 1.1 Professional 0 May 26th, 2004 07:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.