Custom Block Can't Invalid the Cache I wrote a block in Drupal 8 to render a menu based on the http headers of the request, which contains the users' information from the idp. This menu is supposed to be different from user to user, but currently it is cached once it is generated when the very first user login to the website.

I have tried setting the cache max-age to 0, but it didn't seem to work at all. Then I tried the session of the cache contexts, but still those pages visited by previous users are cached. Neither did header:custid.

Here is a sample code

class MyBlock extends BlockBase{
  public function build () {
    return array(
      '#cache'=&gt;array('contexts'=&gt;array('session')),
     '#markup' =&gt; ...,
    );
  }

 ...
}


Thanks in advance for any help!
I wrote a block in Drupal 8 to render a menu based on the http headers
  of the request, which contains the users' information from the idp.


If you want to build a new page for every request then there is no point in using the page cache, so I would uninstall the core module Internal Page Cache.

Edit: Just read in drupal planet, BigPipe will be available in Drupal 8.1, then you have a perfect solution for this. But in the meantime running a site without page cache for anonymous users is fine, you still have the dynamic cache and can use the cache tags.