验证密码登录始终为false使用Yii 2 [英] validate password for login always false using yii 2
问题描述
这是模型/ user.php的我的
validatePassword
函数
This is my validatePassword
function in models/user.php
public function validatePassword($password)
{
/* var_dump(Yii::$app->getSecurity()->validatePassword($password, $this->password)); */
return Yii::$app->getSecurity()->validatePassword($password, $this->password);
};
这是模型/ user.php的我的
setPassword
函数
this is my setPassword
function in models/user.php
public function setPassword($password)
{
$this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
}
调用 setPassword
功能我用下面的模型/ user.php的
: beforeSave
功能
to call setPassword
function I use following beforeSave
function in models/user.php
:
public function beforeSave($insert) {
$this->setPassword($this->password);
$this->generateAuthKey();
$this->generatePasswordResetToken();
return true;
}
示范规则模型/ user.php的
public function rules()
{
return [
[['username', 'password', 'role'], 'required'],
[['username', 'password'], 'string', 'max' => 45],
[['role'], 'string', 'max' => 16],
[['auth_key'], 'string', 'max' => 32],
[['password_reset_token'], 'string', 'max' => 255],
[['username'], 'unique']
];
}
为什么我的密码验证总是假
?
推荐答案
我觉得你做在数据库中的错误。也许,你的密码字段有VARCHAR(32),(你写了这是正常的,当我使用MD5密码)。
I think you made a mistake in the database. Perhaps, your password field has VARCHAR(32) (you wrote "it's normal when i used md5 password.").
Yii::$app->getSecurity()->generatePasswordHash($password);
这个函数生成字符串的长度超过32个字符。对于密码字段,请使用文本,而不是VARCHAR(32)。
This function generates the string is longer than 32 characters. For the password field, use the TEXT instead of VARCHAR(32).
这篇关于验证密码登录始终为false使用Yii 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!