Tuesday, February 26, 2008

ColdFusion Active Directory Authentication

My web server is off-site, completely disconnected from our primary network.  As we work to build our portal, we needed to figure out a way to authenticate our in-house users with a single sign-on.   Of course there are lots of LDAP/AD options out there, but that requires holes in the firewall.  Something my company cannot do.

So, after a little head scratching, I decided to try out the <cfexchangeconnection tags that come bundled with CF8.

I created the following function to auth to our AD by way of our M$ Exchange OWA... The only thing that was tricky was making sure the SSL Certificate was happy with ColdFusion.  Rather than dealing with the CACERTS file again, we just bought a well-known authority cert for our webmail server.  

Here's the tiny little function.  Give this a try.  If you want to get more info out of AD, try Boyan Kostadinov's AD/SQL Server idea.  Use the cfexchange methods to auth, and access his method by way of SQL Server 2005 webservice (or direct connection if you have it) to get a complete user profile... 

<cffunction name="authADUser" returnType="query">
<cfargument name="UserID" type="string" required="false" default="0">
<cfargument name="Passwd" type="string" required="false" default="0">

<cfset var newQueryObject = queryNew("ExchangeHost,MailBoxName") />
<cfset ExchangeHost = "webmail.yourcompany.com">
<CFTRY>

<cfexchangeConnection
action="open"
username="#lcase(UserID)#"
password="#Passwd#"
server="#ExchangeHost#"
protocol="https"
connection="exchangeConnection">


<cfset queryAddRow(newQueryObject, 1) />
<cfset querySetCell(newQueryObject, "ExchangeHost", ExchangeHost) />
<cfset querySetCell(newQueryObject, "MailBoxName", lcase(UserID)) />


<cfexchangeConnection
action="close"
connection="exchangeConnection">

<CFCATCH>
</CFCATCH>
</CFTRY>

<cfreturn newQueryObject>

</cffunction>

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Wonderful solution! As someone who doesn't know much about SAML, and wants a quick and easy way to do single-sign on through Active Directory, this saved me tonnes of time, thank you.