User Guide
310 Chapter 2: ColdFusion Tags
Save the following page as loginform.cfm:
<!--- A simple login form that posts back to the page whose request initiated
the login. --->
<H2>Please Log In</H2>
<cfform action="#CGI.script_name#">
<!--- j_username and j_password are special names that populate cflogin tag
variables. --->
User Name: <cfinput type="text" name="j_username" value="cfqa_user1"
required="Yes"><br>
Password: <cfinput type="password" name="j_password" value="cfqa_user1"
required="Yes"><br>
Domain: <cfinput type="text" name="domain" value="rnd" required="Yes"><br>
<input type="submit" value="Log In">
</cfform>
Save the following page as Application.cfm:
<!--- If this page is executing in response to the user clicking a logout link,
log out the user. The cflogin tag code will then run. --->
<cfif IsDefined("URL.logout") AND URL.logout>
<cflogout>
</cfif>
<!--- The cflogin body code runs only if a user is not logged in. --->
<cflogin>
<!--- cflogin variable exists only if login credentials are available. --->
<cfif NOT IsDefined("cflogin")>
<!--- Show a login form that posts back to the page whose request
initiated the login, and do not process the rest of this page. --->
<cfinclude template="loginform.cfm">
<cfabort>
<cfelse>
<!--- Trim any leading or trailing spaces from the username and password
submitted by the form. --->
<cfset theusername=trim(form.j_username)>
<cfset thepassword=trim(form.j_password)>
<cfset thedomain=trim(form.domain)>
<cfntauthenticate username="#theusername#" password="#thepassword#"
domain="#thedomain#" result="authresult" listgroups="yes">
<!--- authresult.auth is True if the user is authenticated. --->
<cfif authresult.auth>
<!--- Log user in to ColdFusion and set roles to the user's Groups. --->
<cfloginuser name="#theusername#" password="#thepassword#"
roles="authresult.groups">
<cfelse>
<!--- The user was not authenticated.
Display an error message and the login form. --->
<cfoutput>
<cfif authresult.status IS "AuthenticationFailure">
<!--- The user is valid, but not the password. --->
<H2>The password for #theusername# is not correct<br>
Please Try again</H2>
<cfelse>
<!--- There is one other status value, invalid user name. --->
<H2>The user name #theusername# is not valid<br>