CakePHP $this->Auth->login() 不起作用

标签 cakephp cakephp-1.3 authentication

除了授权手动调用外,网络应用程序运行良好。我已经为此苦苦挣扎了好几天。在我的示例代码中,我暂时重写了 cookie 以缩小原因范围。这是我的应用程序 Controller 片段:

App::import('Sanitize');
//uses('sanitize');
class AppController extends Controller {
    var $components = array('Clean','Acl', 'Auth', 'Session', 'RequestHandler', 'Cookie', /* 'DebugKit.Toolbar' */);
    var $helpers = array('uiNav','Flash','Html', 'Form', 'Session','Javascript','Ajax','Js' => array('Jquery'), 'Time','Js');

    function beforeFilter() {
        //Configure AuthComponent
        $this->Auth->authorize = 'actions'; 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'view');
        $this->Auth->actionPath = 'controllers/';
        $this->Auth->autoRedirect = false;

        $this->Auth->allowedActions = array('display');

        if(!$this->Auth->user()){
            //$cookie = $this->Cookie->read('Auth.User');
            $cookie = array('username' => 'chris22', 'password' => 'stuff');
            if (!is_null($cookie)) {
                $this->set('checking_cookie',$cookie);
                if ($this->Auth->login($cookie)) {
                    $this->set('cookie_message','cookie validates!');
                    //  Clear auth message, just in case we use it.
                    $this->Session->delete('Message.auth');
    /*              $this->redirect($this->Auth->redirect()); */
                }
            }
        }
    }
}

如您所见,我只是将用户名和密码插入 $this->Auth->login 中,但它不起作用!!

我不知道我的用户 Controller 是否相关,但这也是登录功能:

function login() {
    if ($this->Auth->user()) {
        if (!empty($this->data) && $this->data['User']['remember_me'] && isset($this->data['User']['password'])) {
            $cookie = array();
            $cookie['username'] = $this->data['User']['username'];
            $cookie['password'] = $this->data['User']['password'];
            $this->Cookie->write('Auth.User', $cookie, true, '+1 month');
            //unset($this->data['User']['remember_me']);
            $this->set('cookie-00', 'setting cookie.');

        }else{ $this->set('cookie_message', 'not setting cookie.');}

        $this->redirect($this->Auth->redirect());
    }

}    

谢谢!

最佳答案

编辑 - 1 - 我想我知道为什么这对你不起作用。

原因1:$this->Auth->login以的形式获取数据

array(
    'User'=>array(
        'username'=>'myusername',
        'password'=>'mypassword'
    )
)

原因 2:$this->Auth->login 不会对密码进行哈希处理。

您发送的密码必须与数据库中显示的完全一致。

这条线以下的一切都是可能的,但在这种情况下不太可能:

您确定在最初创建用户名和密码时设置了哈希值吗?

  1. 要检查您的哈希值是否匹配,请使用 phpmyadmin 或 mysql 工作台查看您的用户表,并找到用户 chris22 的密码字段
  2. 将该条目与当前的哈希进行比较。要检查当前的哈希值,请将代码放在 Controller 函数(索引)中的某个位置并导航到那里。

    debug(Security::hash('stuff'));
    exit;
    

希望这会有所帮助!

关于CakePHP $this->Auth->login() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8461425/

相关文章:

CakePHP 3案例计数查询返回true而不是整数

php - CakePHP 格式在 View 中找到 ('all' ) 到 ('list' )?

php - CakePHP错误: Database table acos for model Aco was not found

windows - 使用 IIS 和应用程序池的 WCF Windows 身份验证

authentication - 如何在 django channel 上使用 Knox token 身份验证对 websocket 连接进行身份验证?

validation - CakePHP 中的模型验证检查是否至少设置了其中之一

php - 我的 json 网络服务意外地产生了斜杠

Cakephp 主页逻辑

cakephp - Cake 网站在将其移动到我的本地主机后找不到图像或 CSS 文件?

ios - 确认 iPhone 用户