13

I try to authenticate user:

<?php /** * @Route("/testLogin", name="testLogin") */ public function testLoginAction() { $em = $this->getDoctrine()->getEntityManager(); $user = $em->getRepository('ApplicationDefaultBundle:User')->findOneBy(array('id' => 126)); $providerKey = 'main'; $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles()); $this->container->get('security.context')->setToken($token); return $this->redirect($this->generateUrl('testCheck')); } /** * @Route("/testCheck", name="testCheck") */ public function testCheckAction() { if (false === $this->get('security.context')->isGranted( 'IS_AUTHENTICATED_REMEMBERED' )) { return new Response('Not logged'); } $user = $this->container->get('security.context')->getToken()->getUser(); return new Response($user->getUsername.' is logged'); } 

But I get permanent 302 redirect to /login page.

security: encoders: Application\Bundle\DefaultBundle\Entity\User: algorithm: sha512 iterations: 24 encode_as_base64: true role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SPECIALIST: ROLE_USER ROLE_EMPLOYER: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] providers: main: entity: { class: Application\Bundle\DefaultBundle\Entity\User, property: username } firewalls: secured_area: remember_me: key: MySecretKeyBlablabla lifetime: 36000000 path: / domain: ~ pattern: ^/ form_login: check_path: /login_check login_path: /login provider: main logout: path: /logout target: / anonymous: true access_control: - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 

Code for authenticate I take from https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php

Error in app/logs/dev.log:

redirecting to authentication entry point (No Authentication Provider found for token of class "Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken".) [] [] 

I can't access to site until I clean cookies.

2 Answers 2

16

Try code from this answer. In your case firewall name is secured_area:

// your controller action public function myAction() { // Authenticating user $token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles()); $this->get('security.token_storage')->setToken($token); //For Symfony <= 2.3 //$this->get('security.context')->setToken($token); $this->get('session')->set('_security_secured_area', serialize($token)); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Replace security.context with security.token_storage in current Symfony versions.
0

Verify that you have configured encoders section in security.yml, as configuration there has changed not very long time ago and it affects your problem.

1 Comment

I added to my question full security.yml

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.