CakePHP$this->;认证->;登录()不工作

CakePHP$this->;认证->;登录()不工作,cakephp,cakephp-1.3,authentication,Cakephp,Cakephp 1.3,Authentication,除了auth手动调用之外,这个web应用程序工作得很好。我已经为此挣扎了好几天了。在我的示例代码中,我临时重写了cookie以缩小原因。这是我的应用程序控制器剪报: App::import('Sanitize'); //uses('sanitize'); class AppController extends Controller { var $components = array('Clean','Acl', 'Auth', 'Session', 'RequestHandler', '

除了auth手动调用之外,这个web应用程序工作得很好。我已经为此挣扎了好几天了。在我的示例代码中,我临时重写了cookie以缩小原因。这是我的应用程序控制器剪报:

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,但它不起作用

我不知道我的用户控制器是否相关,但这里也有相应的登录功能:

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不会散列密码。

您必须发送与数据库中显示完全相同的密码

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

您确定最初创建用户名和密码时已设置哈希吗

  • 要检查散列是否匹配,请使用phpmyadmin或mysql workbench查看用户表,并找到用户chris22的密码字段
  • 将该条目与当前哈希进行比较。要检查当前哈希,请将下面的代码放在控制器函数(索引)的某个位置,然后导航到该位置

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

  • 我希望这有帮助

    确保在Auth组件中正确分配了字段。 如果使用不同于用户名和密码的字段名进行连接,则必须在控制器中按以下方式声明:

    'Auth' => array(
      'authenticate' => array(
        'Form' => array(
          'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
        )
      )
    )
    

    编辑1帮助了我。谢谢