How to restore $form_state when rebuilding a form after a redirect We have a site where either authenticated or unauthenticated users can submit a form. When the form is validated, we check whether they are logged-in, and if not, we redirect them to authenticate, with a destination parameter of the original page. At this point, we cache the $form and $form_state in $_SESSION.

After authentication and redirection the form is redisplayed on the page (by retrieving the $form from $_SESSION) and ask them to confirm the submit.
Note the form is dynamically-generated which is why we have to store it.

What I can't seem to do on redisplay is set the form values to the choices that the user previously made, even though these are stored in the cached $form_state. What is the best way to do this?
I got this working in the end. The solution was:

$form_array = drupal_retrieve_form($cached_form, $cached_form_state);
$form = $form_array['#form_id'];
$block['content'] = drupal_render($form);


where $cached_form and $cached_form_state were the $form and $form_state values that had been stored in $_SESSION. Note that '#form_id' is a literal string, I've no idea why drupal_retrieve_form returns an array with that string as a key. 

This redisplayed the form in the block after the authentication challenge, with the user's pre-challenge selections populated into the form elements. I couldn't get it to work with either drupal_get_form() or drupal_build_form().