Whatever message this page gives is out now! Go check it out!
<cfif sUserName eq uUserName AND sPassword eq uPassword>
<cfset retargs.authenticated="YES">
<cfelse>
<cfset retargs.authenticated="NO">
</cfif>
<cfreturn retargs><cfquery name="loginQuery" dataSource="#Application.DB#" >
SELECT *
FROM Users
WHERE UserName = <cfqueryparam value="#uUserName#" CFSEQLType=
'CF_SQL_VARCHAR'AND password = <cfqueryparam value="#uPassword#"
CFSEQLType='CF_SQL_VARCHAR'>
</cfquery>
<cfif loginQuery.recordcount gt 0>
<cfset retargs.authenticated="YES">
<cfset retargs.roles=loginQuery.roles>
<cfelse>
<cfset retargs.authenticated="NO">
</cfif>
<cfreturn retargs><cfcomponent>
<cfset This.name = "Orders">
<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>
<cflogin>
<cfif IsDefined("cflogin")>
<cfif cflogin.name eq "admin">
<cfset roles = "user,admin">
<cfelse>
<cfset roles = "user">
</cfif>
<cfloginuser name = "#cflogin.name#" password = "#cflogin.password#"
roles = "#roles#" />
<cfelse>
<!--- This should never happen. --->
<h4>Authentication data is missing.</h4>
Try to reload the page or contact the site administrator.
<cfabort>
</cfif>
</cflogin>
</cffunction>
</cfcomponent>Code | Description |
<cfcomponent>
<cfset This.name = "Orders">
<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>Identifies the application and starts the onRequestStart method that runs at the starts of each request. The login information on this page only applies to this application. | |
<cflogin>
<cfif IsDefined("cflogin")>
<cfif cflogin.name eq "admin">
<cfset roles = "user,admin">
<cfelse>
<cfset roles = "user">
</cfif>Executes if there is no logged-in user.Makes sure that the user is correctly logged in by the web server. (Otherwise, there would be no cflogin variable.)Sets a roles variable based on the user's ID. Assigns users named "admin" to the admin role. Assigns all other users to the users role. | |
<cfloginuser name = "#cflogin.name#" password = "#cflogin.password#" roles = "#roles#" /> | Logs the user into the ColdFusion security system and specifies the user's password, name, and roles. Gets the password and name directly from the cflogin structure. |
<cfelse>
<!--- This should never happen. --->
<h4>Authentication data is missing.</h4>
Try to reload the page or contact the site administrator.
<cfabort>This code should never run, but if the user somehow got to this page without logging in to the web server, this message would display and ColdFusion would stop processing the request. | |
</cfif>
</cflogin>
</cffunction>
</cfcomponent>Ends the if/else block.Ends the cflogin tag body.Ends the onRequestStart method.Ends the Application component. |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Basic authentication security test page</title>
</head>
<body>
<cfoutput>
<h2>Welcome #GetAuthUser()#!</h2>
</cfoutput>
ALL Logged-in Users see this message.<br>
<br>
<cfscript>
if (IsUserInRole("admin"))
WriteOutput("Users in the admin role see this message.<br><br>");
if (IsUserInRole("user"))
WriteOutput("Everyone in the user role sees this message.<br><br>");
</cfscript>
</body>
</html>Code | Description |
<cfoutput>
<h2>Welcome #GetAuthUser()#!</h2>
</cfoutput>User is already logged in by Application.cfc. Displays a welcome message that includes the user's login ID. | |
ALL Logged-in Users see this message.<br>
<br>Displays this message in all cases. The page does not display until a user is logged in. | |
<cfscript>
if (IsUserInRole("admin"))
WriteOutput("Users in the admin role see this message.<br><br>");
if (IsUserInRole("user"))
WriteOutput("Everyone in the user role sees this message.<br><br>");
</cfscript>Tests whether the user belongs to each of the valid roles. If the user is in a role, displays a message with the role name.The user sees one message per role to which the user belongs. |
UserID | Password | Roles |
BobZ | Ads10 | Employee,Sales |
JaniceF | Qwer12 | Contractor,Documentation |
RandalQ | ImMe | Employee,Human Resources,Manager |
<cfcomponent>
<cfset This.name = "Orders">
<cfset This.Sessionmanagement="True">
<cfset This.loginstorage="session">
<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>
<cfif IsDefined("Form.logout")>
<cflogout>
</cfif>
<cflogin>
<cfif NOT IsDefined("cflogin")>
<cfinclude template="loginform.cfm">
<cfabort>
<cfelse>
<cfif cflogin.name IS "" OR cflogin.password IS "">
<cfoutput>
<h2>You must enter text in both the User Name
and Password fields.
</h2>
</cfoutput>
<cfinclude template="loginform.cfm">
<cfabort>
<cfelse>
<cfquery name="loginQuery" dataSource="cfdocexamples">
SELECT UserID, Roles
FROM LoginInfo
WHERE
UserID = '#cflogin.name#'
AND Password = '#cflogin.password#'
</cfquery>
<cfif loginQuery.Roles NEQ "">
<cfloginuser name="#cflogin.name#" Password = "#cflogin.password#"
roles="#loginQuery.Roles#">
<cfelse>
<cfoutput>
<H2>Your login information is not valid.<br>
Please Try again</H2>
</cfoutput>
<cfinclude template="loginform.cfm">
<cfabort>
</cfif>
</cfif>
</cfif>
</cflogin>
<cfif GetAuthUser() NEQ "">
<cfoutput>
<form action="securitytest.cfm" method="Post">
<input type="submit" Name="Logout" value="Logout">
</form>
</cfoutput>
</cfif>
</cffunction>
</cfcomponent>| Code | Description |
<cfcomponent>
<cfset This.name = "Orders">
<cfset This.Sessionmanagement="True">
<cfset This.loginstorage="session">
<cffunction name="OnRequestStart">
<cfargument name = "request" required="true"/>| Identifies the application, enables session management, and enables storing login information in the Session scope.Begins the definition of the onRequestStart method that runs at the starts of each request. | |
<cfif IsDefined("Form.logout")>
<cflogout>
</cfif> If the user just submitted the logout form, logs out the user. The following cflogin tag runs as a result. | |
<cflogin>
<cfif NOT IsDefined("cflogin")>
<cfinclude template="loginform.cfm">
<cfabort>| Runs if there is no logged-in user.Tests to see if the user has submitted a login form. If not, uses cfinclude to display the form. The built-in cflogin variable exists and contains the user name and password only if the login form used j_username and j_password for the input fields.The cfabort tag prevents processing of any code that follows on this page. | |
<cfelse>
<cfif cflogin.name IS "" OR cflogin.password IS "">
<cfoutput>
<h2>You must enter text in both the User Name and Password fields. </h2>
</cfoutput>
<cfinclude template="loginform.cfm">
<cfabort>| Runs if the user submitted a login form. Tests to make sure that both name and password have data. If either variable is empty, displays a message, followed by the login form. The cfabort tag prevents processing of any code that follows on this page. | |
<cfelse>
<cfquery name="loginQuery" dataSource="cfdocexamples">
SELECT UserID, Roles
FROM LoginInfo
WHERE
UserID = '#cflogin.name#'
AND Password = '#cflogin.password#'
</cfquery>| Runs if the user submitted a login form and both fields contain data. Uses the cflogin structure's name and password entries to find the user record in the database and get the user's roles. | |
<cfif loginQuery.Roles NEQ "">
<cfloginuser name="#cflogin.name#" Password = "#cflogin.password#" roles="#loginQuery.Roles#">| If the query returns data in the Roles field, logs in the user using the user's name and password and the Roles field from the database. In this application, every user must be in some role. | |
<cfelse>
<cfoutput>
<H2>Your login information is not valid.<br>
Please Try again</H2>
</cfoutput>
<cfinclude template="loginform.cfm">
<cfabort>| Runs if the query did not return a role. If the database is valid, this means there was no entry matching the user ID and password. Displays a message, followed by the login form. The cfabort tag prevents processing of any code that follows on this page. | |
</cfif>
</cfif>
</cfif>
</cflogin> Ends the loginquery.Roles test code.Ends the form entry empty value test.Ends the form entry existence test.Ends the cflogin tag body. | |
<cfif GetAuthUser() NEQ "">
<cfoutput>
<form action="securitytest.cfm" method="Post">
<input type="submit" Name="Logout" value="Logout">
</form>
</cfoutput>
</cfif>If a user is logged-in, displays the Logout button.If the user clicks the button, posts the form to the application's (theoretical) entry page, index.cfm. Application.cfc then logs out the user and displays the login form. If the user logs in again, ColdFusion displays index.cfm. | |
</cffunction>
</cfcomponent>| Ends the onRequestStart methodEnds the Application component. |
<H2>Please Log In</H2>
<cfoutput>
<form action="#CGI.script_name#?#CGI.query_string#" method="Post">
<table>
<tr>
<td>user name:</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>Code | Description |
<H2>Please Log In</H2>
<cfoutput>
<form action="#CGI.script_name#?#CGI.query_string#" method="Post">
<table>
<tr>
<td>user name:</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>Displays the login form. Constructs the form action attribute from CGI variables, with a ? character preceding the query string variable. This technique works because loginform.cfm is accessed by a cfinclude tag on Application.cfc, so the CGI variables are those for the originally requested page.'The form requests a user ID and password and posts the user's input to the page specified by the newurl variable.Uses the field names j_username and j_password. ColdFusion automatically puts form fields with these values in the cflogin.name and cflogin.password variables inside the cflogin tag. |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Security test page</title>
</head>
<body>
<cfoutput>
<h2>Welcome #GetAuthUser()#!</h2>
</cfoutput>
ALL Logged-in Users see this message.<br>
<br>
<cfscript>
if (IsUserInRole("Human Resources"))
WriteOutput("Human Resources members see this message.<br><br>");
if (IsUserInRole("Documentation"))
WriteOutput("Documentation members see this message.<br><br>");
if (IsUserInRole("Sales"))
WriteOutput("Sales members see this message.<br><br>");
if (IsUserInRole("Manager"))
WriteOutput("Managers see this message.<br><br>");
if (IsUserInRole("Employee"))
WriteOutput("Employees see this message.<br><br>");
if (IsUserInRole("Contractor"))
WriteOutput("Contractors see this message.<br><br>");
</cfscript>
</body>
</html>Code | Description |
<cfoutput> <h2>Welcome #GetAuthUser()#!</h2> </cfoutput> | Displays a welcome message that includes the user's login ID. |
ALL Logged-in Users see this message.<br>
<br>Displays this message in all cases. The page does not display until a user is logged in. | |
<cfscript>
if (IsUserInRole("Human Resources"))
WriteOutput("Human Resources members see this message.<br><br>");
if (IsUserInRole("Documentation"))
WriteOutput("Documentation members see this message.<br><br>");
if (IsUserInRole("Sales"))
WriteOutput("Sales members see this message.<br><br>");
if (IsUserInRole("Manager"))
WriteOutput("Managers see this message.<br><br>");
if (IsUserInRole("Employee"))
WriteOutput("Employees see this message.<br><br>");
if (IsUserInRole("Contractor"))
WriteOutput("Contractors see this message.<br><br>");
</cfscript>Tests whether the user belongs to each of the valid roles. If the user is in a role, displays a message with the role name.Users see one message per role that they belong. |
<cfapplication name="Orders" sessionmanagement="Yes" loginstorage="Session">
<cflogin>
<cfif isDefined("cflogin")>
<!--- setting basic attributes --->
<cfset LDAP_root = "o=mycompany.com">
<cfset LDAP_server = "ldap.mycompany.com">
<cfset LDAP_port = "389">
<!--- Create the prefix and suffix parts of the user's DN. --->
<cfset userPrefix = "cn=">
<cfset userSuffix = ",ou=Users,o=mycompany.com">
<!--- Concatenate the user's DN and use it to authenticate. --->
<cfset LDAP_username = userPrefix&cflogin.name&userSuffix>
<!--- This filter will look for groups for containing the user's ID. --->
<cfset userfilter =
"(&(objectClass=groupOfUniqueNames)
(uniqueMember=#LDAP_username#))">
<!--- Search for groups containing the user's dn.
The groups represent the user's roles.
NOTE: Your LDAP permissions must allow authenticated users to search.
groups. --->
<cftry>
<cfldap action="QUERY"
name="auth"
attributes="cn"
referral="yes"
start="#LDAP_root#"
scope="SUBTREE"
server="#LDAP_server#"
port="#LDAP_port#"
filter="#userfilter#"
username="#LDAP_username#"
password="#cflogin.password#"
>
<cfcatch type="any">
<cfif FindNoCase("Invalid credentials", cfcatch.detail)>
<cfoutput>
<script>alert("User ID or
Password invalid for user:
#cflogin.name#")</script>
</cfoutput>
<cfabort>
<cfelse>
<cfoutput>
<script>alert("Unknown error for user: #cflogin.name#
#cfcatch.detail#")</script>
</cfoutput>
<cfabort>
</cfif>
</cfcatch>
</cftry>
<!--- If the LDAP query returned a record, the user is valid. --->
<cfif auth.recordcount>
<cfloginuser name="#cflogin.name#" password="#cflogin.password#"
roles="#valueList(auth.cn)#">
</cfif>
</cfif>
</cflogin>Code | Description |
<cflogin>
<cfif isDefined("cflogin")>
<!--- setting basic attributes --->
<cfset LDAP_root = "o=mycompany.com">
<cfset LDAP_server = "ldap.mycompany.com">
<cfset LDAP_port = "389">
<!--- Create the prefix and suffix parts of the user's DN. --->
<cfset userPrefix = "cn=">
<cfset userSuffix = ",ou=Users,o=mycompany.com">
<!--- Concatenate the user's DN and use it to authenticate. --->
<cfset LDAP_username = userPrefix&cflogin.name&userSuffix>
<!--- This filter will look for groups for containing the user's ID. --->
<cfset userfilter = "(&(objectClass=groupOfUniqueNames)(uniqueMember=#LDAP_username#))">Starts the cflogin tag body. Sets several variables to the values used as attributes in the cfldap tag. Sets prefix and suffix values used to create a distinquished name (dn) for binding to the LDAP server. Creates the user's bind dn by concatenating the prefix and suffix with cflogin .name. This variable is used for authenticating the user to the LDAP server.Sets the filter used to search the directory and retrieve the user's group memberships. The group membership represents the user's roles within the organization. | |
<cftry>
<cfldap action="QUERY"
name="auth"
attributes="cn"
referral="yes"
start="#LDAP_root#"
scope="SUBTREE"
server="#LDAP_server#"
port="#LDAP_port#"
filter="#userfilter#"
username="#LDAP_username#"
password="#cflogin.password#"
>In a cftry block, uses the user's concatenated dn to authenticate to the LDAP server and retrieve the common name (cn) attribute for groups to which the user is a member. If the authentication fails the LDAP server returns an error. Note: The LDAP permissions must allow an authenticated user to read and search groups in order for the query to return results. | |
<cfcatch type="any">
<cfif FindNoCase("Invalid credentials", cfcatch.detail)>
<cfoutput>
<script>alert("User ID or Password invalid for user: #cflogin.name#")</script>
</cfoutput>
<cfabort>
<cfelse>
<cfoutput>
<script>alert("Unknown error for user: #cflogin.name# #cfcatch.detail#")</script>
</cfoutput>
<cfabort>
</cfif>
</cfcatch>
</cftry>Catches any exceptions. Tests to see if the error information includes the string "invalid credentials", which indicates that either the dn or password is invalid. If so, displays a dialog box with an error message indicating the problem. Otherwise, displays a general error message.If an error is caught, the cfabort tag ends processing of the request after displaying the error description.End of cfcatch and cftry blocks. | |
<cfif auth.recordcount>
<cfloginuser name="#cflogin.name#" password="#cflogin.password#"
roles="#valueList(auth.cn)#">
</cfif>
</cfif>
</cflogin>If the authorization query returns a valid record, logs in the user. Uses the valueList function to create a comma-separated list of the users retrieved group memberships, and passes them in the cfloginuser roles attribute.Ends the initial isDefined("cflogin") cfif block .Ends the cflogin tag body |