Symfony2 : remember me cookie gets deleted when reopening the browser

一个人想着一个人 提交于 2020-01-02 07:46:51

问题


Despite of all the other questions on stack-overflow I was not able to resolve the issue with all the provided information.
That's why I decided to create a new one.

So I am implementing the remember me function in my login form with a checkbox that looks the like this:

<input type="checkbox" id="form_saveCredentials" name="form[saveCredentials]">

On login everything looks fine and the cookie gets set correctly

Through the whole browser session the cookie remains alive.
But when I close the browser and reopen it the cookie is still there (did not navigate to my localhost yet!).

When I navigate to my website on localhost the cookie gets deleted according to the response header

I have no idea why the cookie gets deleted on the navigation to the website.

Maybe the problem lies in the securtiy.yml settings for the remember_me functionality

firewalls:
    somefirewall:
        form_login:
            remember_me: true
remember_me:
        key: %secret%
        lifetime: 31536000
        remember_me_parameter: 'form[saveCredentials]'
        path: /
        domain: ~

Any idea on how to fix this problem? Thanks in advance


回答1:


I recently had this problem, and here is the solution I found:

The reason why the cookie is deleted is because the user information that the cookie contains doesn't match anything that Symfony knows about. It can't log a user in based on the cookie, so it just deletes it. The issue in my case was an incorrect string in my UserProvider class. The specific method is supportsClass. It was returning 'AppBundle\Security\User' when it should have actually been returning `'AppBundle\Entity\User'. This caused Symfony to be unable to find any users based on the information in the cookie, and then it would just delete the cookie and move on.

You can do some more troubleshooting if you go into this file: vendor/symfony/symfony/src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php. Play around in processAutoLoginCookie and see if you can't figure something out!

Good luck!




回答2:


This problem can occur if your username is a valid e-mail but isn't equal to the e-mail. Remove any @ in usernames.



来源:https://stackoverflow.com/questions/20653772/symfony2-remember-me-cookie-gets-deleted-when-reopening-the-browser

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