如果Else查询为空

时间:2016-01-28 18:27:15

标签: coldfusion

我正在使用CFLDAP让用户使用活动目录进行身份验证。我正在尝试编写if语句,以防用户信息未通过身份验证返回。我认为只要信息正确就可以使用<cfif AuthenticateUser.RecordCount gt 0>进行检查,但是如果输入了错误的信息并且没有进行任何身份验证,则它不会运行else语句。任何有关这方面的帮助将不胜感激!

<cfldap action="query"
              name="AuthenticateUser"
              attributes="dn,mail,givenname,sn,samaccountname,memberof"
              start="DC=domain,DC=net"
              filter="(&(objectclass=user)(samAccountName=#trim(form.user_name)#))"
              server="servername"
              Port="389"
              username="tc\#trim(form.user_name)#" 
              password="#trim(form.user_pass)#">
<cfoutput>#AuthenticateUser.RecordCount#</cfoutput>
<!--- Get all records from the database that match this users credentials ---> 
<cfquery name="userVerify" datasource="test">
    SELECT  *
    FROM    dbo.Users
    WHERE   user_name = <cfqueryparam value="#AuthenticateUser.samaccountname#" cfsqltype="cf_sql_varchar" />
</cfquery>
<cfif AuthenticateUser.RecordCount gt 0> 
    <!--- This user has logged in correctly, change the value of the session.allowin value ---> 
    <cfset session.allowin = "True" /> 
    <cfset session.employee_number = userVerify.employee_number /> 

     <!--- Now welcome user and redirect to "index.html" ---> 
    <script>  
        self.location="../dashboard/dashboard.cfm"; 
    </script> 
<cfelse> 
    <!--- this user did not log in correctly, alert and redirect to the login page ---> 
    <script> 
        alert("Your credentials could not be verified, please try again!"); 
        self.location="Javascript:history.go(-1)"; 
    </script> 
    </cfif> 

我也尝试过:<cfif len(AuthenticateUser)> enter image description here

3 个答案:

答案 0 :(得分:2)

这是一种格式化的评论。你想要立刻做太多。一步一步走。从这开始:

<cfdump var="before cfldap tag<br />">

<cfldap action="query"
          name="AuthenticateUser"
 etc
 >
<cfdump var="after cfldap tag<br />">
<cfdump var = "#AuthenticateUser#">
<cfdump var="after cfdump<br />">

使用有效和无效凭据运行此代码。看看你得到了什么。相应的反应。

答案 1 :(得分:2)

我就是这样做的。我尝试使用提供的用户名和密码对我们的域运行查询。如果提供的用户名和密码无效,则会生成错误。

<cftry>
    <cfldap action="Query"
        name="ADResult"
        attributes="dn"
        start="DC=domain,DC=net"
        filter="sAMAccountName=administrator" 
        server="servername"
        scope = "subtree"
        username="#arguments.username#"
        password="#arguments.password#" />

    <cfset isAuthenticated = true />
<cfcatch type="any">
    <cfset isAuthenticated = false />
</cfcatch>
</cftry>

<cfreturn isAuthenticated />

我将它包装在一个名为“authenticate”的函数中,并通过我从我的应用程序调用的Web服务公开它。如果我需要有关用户的其他详细信息(mail,givenName等),我确定用户已经过身份验证后,我将在同一个Web服务中使用另一个函数。请注意,在这个其他功能中,我使用管理员用户名和密码来运行查询。

<cfldap action="Query"
    name="ADResult"
    attributes="mail,givenName"
    start="DC=domain,DC=net"
    filter="sAMAccountName=#arguments.username#" 
    server="servername"
    scope = "subtree"
    username="administrator"
    password="myAdminPassword" />

我获取结果,填充查询对象或结构,并将其返回给调用函数。

所以整个过程看起来像这样:

<cfset objAD = createobject("webservice", "http://mywebservice.com") />
<cfset isAuthenticated = objAD.authenticate(form.username, form.password) />
<cfif isAuthenticated>
    <cfset userDetails = objAD.getUserDetails(form.username)>
</cfif>

希望这有帮助。

答案 2 :(得分:1)

我认为在查询失败时会抛出错误。试试这个:

<cftry>
    <cfldap  action="query"
              name="AuthenticateUser"
              attributes="dn,mail,givenname,sn,samaccountname,memberof"
              start="DC=domain,DC=net"
              filter="(&(objectclass=user)(samAccountName=#trim(form.user_name)#))"
              server="servername"
              Port="389"
              username="tc\#trim(form.user_name)#" 
              password="#trim(form.user_pass)#">
   <cfset LoginStatus = "Success">
   <cfcatch type="any">
       <cfset LoginStatus = "Failed">
   </cfcatch>
</cftry>

那么你的cfif会是这样的:

<cfif LoginStatus eq "Success"> 
    <!--- This user has logged in correctly, change the value of the session.allowin value ---> 
    <cfset session.allowin = "True" /> 
    <cfset session.employee_number = userVerify.employee_number /> 

     <!--- Now welcome user and redirect to "index.html" ---> 
    <script>  
        self.location="../dashboard/dashboard.cfm"; 
    </script> 
<cfelse> 
    <!--- this user did not log in correctly, alert and redirect to the login page ---> 
    <script> 
        alert("Your credentials could not be verified, please try again!"); 
        self.location="Javascript:history.go(-1)"; 
    </script> 
</cfif> 

我认为这适用于CF9。