Using CFLDAP to Query or Modify Active Directory
Using CFLDAP to Query Active Directory

The CFLDAP command gives you the ability to query Active Directory to pull out (or insert) information into AD. Once place it has been useful in our organization is for creating an online directory and keeping it up to date. This can also be used to check an account against active directory for authorization. Here’s how it works:

To query active directory based on a person’s username, use the following:

First build a form, call it login.cfm and add to fields:
One textbox called username and the other called password.
Add a submit button and pass the form data to a page called adquery.cfm

Here’s the code for the adquery.cfm page. Put this right at the top of the page:

<cfldap action="query"
           server="servername.domain.net"
           name="Results"
           start="DC=domain,DC=net"
           filter="(&(objectclass=user)(SamAccountName=#form.username#))"
           username="domainname\#form.username#"
           password="#form.password#"
           attributes = "cn,o,l,st,sn,c,mail,telephonenumber, givenname,homephone, streetaddress, postalcode, SamAccountname, physicalDeliveryOfficeName, department">

Action: explain what you want to do
Server: your server
Name: Name of your query
Start: Where your query should start. If you have a large AD, you can search certain OU by adding an OU= in front of the DC part.
Filter: filters out computer accounts by setting it to Objectclass=user. The SamAccountName part says only retrieve records where the Username is what you put in on the form before this page.
Username: This is the username to validate againist the server with. In this case, I am using the info from the form. You can fill this in statically with an account that is used just for this purpose.
Password: Account password.
Attributes: These are the field you want to pull from active directory. Most of them are pretty self explanitory.

Then just use the field on the page like you would use any other database query.

Now, to modify AD with CFLDAP, we change the code a little. First create a form with the field on it you want to modify. In this case, we’ll use mobile phone and postal code. Pass those variable to a page containing the following:

<cfldap action="modify"
           DN="#form.dn#"
           attributes="postalcode=#form.postalcode#;mobile=#form.mobile"
           modifytype="replace"
           server="server.domain.net"
           username="username"
           password="password">

Notice we change the action to modify instead of query.

DN: The Distiningued name of the object you want to modify. Look this one up on Microsoft if you have problems.
Attributes: Fields in AD we wanna modify.
ModifyType: Replace. We are not adding or deleting info, we are just replacing it.
Server: servername
Username: username of the account you want to use to modify AD, not the account you are modifying.
Password: password

Good luck with this guys. It can be a great tool, but can be a pain to get working. The code has to be flawless or you’ll get an error. Look online at a few other sites if you are still having problems.



All ColdFusion Tutorials By Author: Mike Daugherty
  • Adding Multiple Records to a Table and much more.
    Alright, this one is long at first, but once you get the hang of it, it can be really useful. Basically, we are going to create a 3 page application that will allow us to choose the number of people to add to a database, enter these peoples info onto a second page, and add all the records at one time to a database on page three.
    Author: Mike Daugherty
    Views: 15,246
    Posted Date: Wednesday, February 1, 2006
  • Integrating PayPal’s IPN with ColdFusion
    This will let a user buy something from your site using PayPal. Then you will get automatic instant notification of payment. This will also show you how to modify a database to reflect the payment and send the user a receipt.
    Author: Mike Daugherty
    Views: 16,199
    Posted Date: Thursday, July 20, 2006
  • Record Counts of Database Results
    This tutorial is designed to show you how to get a record count of the results returned by your database query. It also show you how to then chart those results.
    Author: Mike Daugherty
    Views: 13,443
    Posted Date: Friday, January 14, 2005
  • Using CFLDAP to Query or Modify Active Directory
    The CFLDAP command gives you the ability to query Active Directory to pull out (or insert) information into AD. Once place it has been useful in our organization is for creating an online directory and keeping it up to date. This can also be used to check an account against active directory for authorization
    Author: Mike Daugherty
    Views: 17,320
    Posted Date: Wednesday, January 12, 2005