title: Symfony authentication api keys lost sessions
body: <p>I am following this guide <a href="http://symfony.com/doc/current/cookbook/security/api_key_authentication.html" rel="nofollow">http://symfony.com/doc/current/cookbook/security/api_key_authentication.html</a> to do the authentication by API KEY.</p>
  
  <p>After calling the last method Authentication token I am able to login a user in, but it doesn't keep the sessions.</p>
  
  <pre><code>return new PreAuthenticatedToken(
          $user,
          $apiKey,
          $providerKey,
          $user-&gt;getRoles()
      );
  </code></pre>
  
  <p>I also tried to do the authentication using this following:</p>
  
  <pre><code>$user = new User("Test", null, array("ROLE_USER"));
  $token = new UsernamePasswordToken($user, null, 'secured_area', $user-&gt;getRoles());
  $this-&gt;get('security.context')-&gt;setToken($token);
  $this-&gt;get('session')-&gt;set('_security_secured_area',serialize($token));
  </code></pre>
  
  <p>Both cases I am only able to log a user in for a single page, not for all project.</p>
  
  <p>Security is like the guide:</p>
  
  <pre><code>firewalls:
      secured_area:
          pattern: ^/testproject
          stateless: true
          simple_preauth:
              authenticator: apikey_authenticator
  </code></pre>
  
  <p>Thanks</p>
  
  <p>[update]</p>
  
  <pre><code>$user = new ApiKeyUserProvider();
  $user = $user-&gt;loadUserByUsername("111111");
  
  $apiKeyProvider = $this-&gt;get('apikey_authenticator');
  $token = $apiKeyProvider-&gt;authenticateToken("111111",$user);
  $this-&gt;get('security.context')-&gt;setToken($token);
  $this-&gt;get('session')-&gt;set('_security_secured_area',serialize($token));
  </code></pre>
  
  <p>loadUserByUsername is like symfony guide.</p>
  
  <p>Thanks</p>