2014-10-17 147 views
0

我使用CakePHP版本2.5.4无效的用户名和密码,登录

问题我无法登录,$这个 - > Auth->登录()返回false每次!

有或没​​有散列密码的相同结果,有什么可能是错误的? 花了一整天检查代码,但是没办法..

我,如果你有时间来检查这里的代码欣赏它:https://gist.github.com/anonymous/2e0c6ab6b3b9a8604893

一切包括:user.php的,UsersController.php,登录.ctp,register.ctp

在此先感谢...

回答

2

我不知道,但我觉得$this->data是不是在CakePHP的2.x版本可用您应该将$this->data更改为$this->request->data,并且所有字段均可用于身份验证。

你在AppController中使用了什么类和hashType?你需要在AppController和Model中使用相同的hashType。

例, 应用程序控制器:

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'passwordHasher' => array(
        'className' => 'Simple', 
        'hashType' => 'sha256' 
       ) 
      ) 
     ) 
    ) 
); 

型号:

public function beforeSave($options = array()) { 
    if (!empty($this->data[$this->alias]['password'])) { 
     $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256')); 
     $this->data[$this->alias]['password'] = $passwordHasher->hash(
      $this->data[$this->alias]['password'] 
     ); 
    } 
    return true; 
} 
+0

他是对的!在模型中使用$ this->数据,在控制器中使用$ this-> request-> data! 功能登录(){ \t如果($这个 - >请求 - >是( '后')){ \t \t如果($这个 - > Auth->登录()){ \t \t \t返回$此 - >重定向($这 - > Auth->重定向()); \t} \t \t $ this-> Session-> setFlash('Invalid Username or Password please try again'); \t} } – 2014-10-17 20:25:23

+0

Controller :: $ data'仍然可用([** via magic properties **](https://github.com/cakephp/cakephp/blob/2.5.4/lib/Cake/Controller) /Controller.php#L395))以达到向后兼容的目的。 – ndm 2014-10-17 21:09:54

1

在AppController的确定包括这样的功能:

public function beforeFilter() { 
    Security::setHash('sha1'); 

} 

你是说你的注册是sha1

现在在你的数据库中你的字段'password'应该是VARCHAR(30)不能超过30个字符。

而且我不舒尔这个线register.ctp,并有一个逗号更后真这是一个错误

<?php echo $this->Form->create('User',array('type' => 'file','novalidate' => true,));?> 

为什么是一个文件?我的事情,只是应该是这个

<?php echo $this->Form->create('User');?> 

和NOVALIDATE可以与真正的数据库进行初始化,则只能升级到新的价值

确定。 我告诉过你现在必须在AppController中添加什么,现在将其添加到UserController中。这就是当你查看表单以多次存储用户信息以使用sha1加密密钥时所发生的情况,这是我们在注册时存储在数据库中的加密。但是,登录时可能会发送另一种类型的加密,并且验证数据进行验证比较a(sha1(密码注册)==其他加密(登录密码))时,通常情况并不相同,这是因为特定的beforeFilter在加密密钥中。

加入这一点,并改变用户控制器:

public function beforeFilter() { 
     parent::beforeFilter(); 
    } 
    public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       if($this->Auth->user('activated')==1){ 
        $this->Session->setFlash(__('Bienvenido, '. $this->Auth->user('username'))); 
        return $this->redirect($this->Auth->redirectUrl()); 
       }else{ 
        $this->Session->setFlash(__('Your Email is not verified. Please verify and try again.')); 
        $this->redirect($this->Auth->logout()); 
       } 
      } else { 
       $this->Session->setFlash(__('Invalid Username or Password please try again')); 
      } 
     } 
    } 
+0

但sha1与密码无关,它将我在激活链接中发送的令牌散列化。 窗体的类型是文件,因为我有一个头像上传字段,但不再是和忘记将其删除,无论如何仍然获得无效的用户名和密码,即使有这些变化 – Exchanger13 2014-10-17 19:58:55

+0

编辑我的答案,我认为它有一些事情要做密码的散列。 – CoolLife 2014-10-17 20:04:15

+0

尝试移动此条件(if($ results ['User'] ['activated'] == 1))to Auth配置范围:http://book.cakephp.org/2.0/en/core-libraries/components /authentication.html#configuring-authentication-handlers 调试您的密码以查看散列表中的密码==散列数据库中的密码http://book.cakephp.org/2.0/en/core-libraries/components /authentication.html#hashing-passwords 使用相同的hashType! – 2014-10-17 20:10:55

0

它实际上发生,因为你可能不使用密码hash.You可以在你的用户模型添加娄代码。

public function beforeSave($options = array()) { 
    if (isset($this->data[$this->alias]['password'])) { 
     $passwordHasher = new SimplePasswordHasher(); 
     $this->data[$this->alias]['password'] = $passwordHasher->hash(
      $this->data[$this->alias]['password'] 
     ); 
    } 
    return true; 
} 

就在您的model.Then添加此代码做出的用户again.If您已经在模型中添加此代码,然后把它保存散列密码database.Is?如果是的话,那么这个错误可能会发生另一个原因,即如果你改变你的安全salt之后让用户。你可以通过建立一个新的用户来解决这个问题。现在的问题你将如何让用户?

在你的AppController中添加

$this->Auth->allow();

在beforeFilter()method.Then在浏览器着呢,你可以访问,使一个新user.After让用户再次尝试login.I认为它会工作。

相关问题