Symfony security.password_encoder isPasswordValid is returning empty

孤者浪人 提交于 2020-02-05 02:51:09

问题


In Symfony 2.6, I am using the following method to encode my password. The password is successfully encoded and saved in the DB.

$encoder = $this->container->get('security.password_encoder');
$encodedPwd = $encoder->encodePassword($adminUser, $plainPassword);

When I try to validate the user supplied password provided in the login form as follows:

$adminUser = $this->getDoctrine()->getManager()->getRepository("AcmeUserBundle:AdminUser")->findOneBy(array('username' => $_username));
$_password = $request->request->get('_password');

$encoder = $this->container->get('security.password_encoder');
echo $encoder->isPasswordValid($adminUser, $_password))

The last line is always returning empty, which means that the password is not getting validated. I have gotten this from Symfony documentation and have searched if anyone has encountered similar problem, but doesn't seem to find any. Can any one please provide some insights please?

Thanks! Sharad


回答1:


Ok I found a solution like this and it is working. Hope it helps anybody encountering similar problem.

For encoding the password, I am doing the following:

$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$encodedPwd = $encoder->encodePassword($plainPassword, $user->getSalt());

For validating the password, I am doing the following:

$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
echo $encoder->isPasswordValid($user->getPassword(), $_password, $user->getSalt())

While encoding the password, I wasn't using the salt, but it is important, It all works now.

Thank you Sharad



来源:https://stackoverflow.com/questions/46877700/symfony-security-password-encoder-ispasswordvalid-is-returning-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!