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



web应用程序运行良好,除了验证手动调用。我已经为此挣扎了好几天了。在我的示例代码中,我临时重写了cookie以缩小原因。这是我的app控制器剪辑:

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());
    }
}    

谢谢!

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

原因1:$this->Auth->login以

形式获取数据
array(
    'User'=>array(
        'username'=>'myusername',
        'password'=>'mypassword'
    )
)

原因2:$this->Auth->login没有哈希密码

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

这行以下的所有内容都是可能的,但在这种情况下不太可能:

你确定最初创建用户名和密码时,哈希值已经设置好了吗?

  1. 要检查你的哈希值是否匹配,请使用phpmyadmin或mysql工作台查看用户表,并找到用户chris22的密码字段
  2. 将该条目与当前散列进行比较。要检查当前的哈希值,请将下面的代码放入控制器函数(索引)并导航到那里。

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

我希望这对你有帮助!

确保在Auth组件中正确分配了字段。如果您使用的字段名与用户名&连接密码,你必须在你的控制器中声明它们,像这样:

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

最新更新