会话 w/CFWheels 不坚持

Sessions w/CFWheels not sticking

我在创建会话值时遇到困难。默认值始终存在。下面是我的初级登录工具。

比较函数工作正常,但在 cflock 和重定向之后,session.userId 和 session.isLoggedIn 仍然分别为 0 和 false。

config/app.cfm

<cfset this.name = "xxx">
<cfset this.sessionManagement = true />
<cfset this.sessionTimeout= createtimespan(0,2,0,0) />
<cfset this.setClientCookies = false />
<cfset this.datasource = "xxx" />

events/onRequestStart.cfc

<cfscript>
    if ( !StructKeyExists(session, "userId") ) {
        session.userId = 0;
        session.isloggedIn = false;
    }
</cfscript>

controllers/admin.cfc

<cfcomponent extends="Controller">
    <cffunction name="init">

    </cffunction>

    <cffunction name="login">

    </cffunction>

    <cffunction name="main">

    </cffunction>

    <cffunction name="login_proc">
        <cfset local.userName = "xxx" />
        <cfset local.password = "yyy" />

        <cfif isPost() and StructKeyExists(params, "username")>
            <cfif compare(params.username, local.username) eq 0 AND compare(params.password, local.password) eq 0>

                <cflock scope="Session" type="exclusive" timeout="3">
                  <cfset session.userId = local.userName />
                  <cfset session.isLoggedIn = true />
                </cflock>

                <cfset redirectTo(action="main")>
            <cfelse>
                <cfset
                    flashInsert(
                        error_msg="Incorrect login."
                )>
                <cfset redirectTo(action="login")>
            </cfif>
        <cfelse>
            <cfset redirectTo(action="login")>
        </cfif>
    </cffunction>
</cfcomponent>

views/admin/login.cfm

<cfform action="/-rootdir-/index.cfm/admin/login_proc" method="post">
        <p>
            <label for="login">Username</label>
            <cfinput type="text" name="username" size="20" required="yes" message="Enter your username" autofocus />
        </p>
        <p>
            <label for="password">Password</label>
            <cfinput type="password" name="password" size="20" required="yes" message="Enter your password" />
        </p>

        <input type="submit" name="login" value="Go" />
    </cfform>

是您 config/app.cfm 中的这一行造成了问题:

<cfset this.setClientCookies = false />

使用 setClientCookies 设置方式,cfidcftokenjsessionid 等 cookie 不会传递给客户端,而 ColdFusion 有重定向发生后没有关于客户端会话的信息。

如果您绝对必须有此设置,那么您需要在所有重定向和链接中传递 cfidcftoken

<cfset redirectTo(action="main", addToken=true)>

<!--- Can't use linkTo anymore unless you override `urlFor` in the controller to use ColdFusion's built-in UrlSessionFormat() function --->
<a href="#UrlSessionFormat(urlFor(action='main'))#>My link</a>

虽然我怀疑你想要这种行为。