Yii2 Exception in ValidatePassword()

2019-08-06 13:53发布

问题:

I use yii2 for developing web site.
Before save user to db, I encripted password like this:

$this->password = Yii::$app->security->generatePasswordHash($this->password);

And when I use this code:

Yii::$app->security->validatePassword("some string", $this->password);

I have error:

Invalid Parameter – yii\base\InvalidParamException Hash is invalid.

What is wrong?

回答1:

I ran into the same issue. I realized that my password field's character capacity was too short for the hash, so I bumped it up from varchar(25) to varchar(255).



回答2:

make sureyou have a column name exactly as password_hash in the user table and change your code from

Yii::$app->security->validatePassword("some string", $this->password);

to:

Yii::$app->security->validatePassword("some string", $this->password_hash);



标签: php yii2