Basic Authentication With ColdFusion
Here is an Application.cfc which implements HTTP Basic Authentication:
<cfcomponent output="false">
<cffunction name="onRequestStart" returnType="boolean" output="false">
<cfargument type="String" name="targetPage" required=true/>
<cfset var authHeader = GetPageContext().getRequest().getHeader("Authorization") />
<cfset var authString = "" />
<cfsetting showDebugOutput="false" />
<cfif IsDefined("authHeader")>
<cfset authString = ToString(BinaryDecode(ListLast(authHeader, " "),"Base64")) />
<cfif GetToken(authString,1,":") eq "username" AND GetToken(authString,2,":") eq "password">
<cfreturn true />
</cfif>
</cfif>
<cfheader statusCode="401" statusText="UNAUTHORIZED" />
<cfheader name="WWW-Authenticate" value="Basic realm=""MXUnit""" />
<cfreturn false />
</cffunction>
</cfcomponent>
The above example has the username and password hard coded as "username" and "password", but it can easily be modified to look up credentials where ever you may have them stored. One thing to note is that I'm using GetPageContext() to get the Authorization header. In my first attempt I was using GetHttpRequestData() which worked fine for normal HTTP requests, but was causing SOAP requests to bomb out. I'm not sure what the issue was because, reading the ColdFusion docs, it seems you should be able to use GetHttpRequestData with SOAP requests:
So, why might you want to use this? Well, I'm using it to secure an mxunit RemoteFacade.cfc on a shared server that I don't administer. (The RemoteFacade.cfc is used by the mxunit Eclipse plugin, which supports basic authentication.)GetHttpRequestData
Description
Makes HTTP request headers and body available to CFML pages. Useful for capturing SOAP request data, which can be delivered in an HTTP header.
One thing to keep in mind about Basic Authentication, it is only secure if you use it over SSL as the username is password are sent over the wire in clear text. So if you do use this approach you may want to put an additional check in your Application.cfc to force secure connections.



Sounds good. I'll have to play around with this stuff.
Sorry if this is a dumb question. But does this mean the CF now supports Basic Server Authentication? Its just that when i had to configure barclays payment solution with a few years back with CF6; Basic Server Authentication was not supported by CF. I had to use PERL.
Then i have test server on local network where I have problem for the exact same site to make authentication.
i know is something silly like variable for the server/location or configuring the CF,or case sensitivity on apache2 linux server vs iis server.
On the apache server where i have problem the login form gets completely skipped and i get unauthorized page.
Please help.
this should be a login form:
<body>
<cfset URLTMP="http://" &"#CGI.Server_name#" & ":" & "#cgi.SERVER_PORT#" & "#CGI.Script_name#">
<cfif CGI.QUERY_STRING is not "">
<CFset URLFIN=URLTMP & "?#CGI.QUERY_STRING#">
<cfelse>
<CFset URLFIN=URLTMP>
</cfif>
<H2>Please Log In</H2>
<cfoutput>
<form action="#urlfin#" method="Post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="j_username"></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="j_password"></td>
</tr>
</table>
<br>
<input type="submit" value="Log In">
</form>
</cfoutput>
</body>
</html>