A typical scenario is where:
- The solution is using forms authentication (using cookies, and with a timeout set to 30 minutes in web.config).
- The user starts filling out a large web form.
- The user takes a long phonecall or goes to lunch.
- The user returns, resumes filling out the form and submits.
- Bang - the user is redirected to the login page because the authentication ticket timed out.
After logging in again the form will be empty - all work filling out the form is lost.
I had no luck googling a solution, so here is what I came up with:
Solving this requires a simple HttpModule and a transit page. The HttpModule will capture the posted form and save it to application state, just before the forms authentication redirects the user to the login page. Application state is used because Session state is not accessible at the time of interception. A few tricks are used here, see the code for details.
After login, the same HttpModule will redirect the request to a transit page. This transit page will restore the form contents as hidden fields and do a submit (http post triggered on load) to the original page. There you go, form restored. The flow looks like this:
This way everything from the original form post will be restored, including ViewState. The original page will happily recieve the postback and do normal page processing. The restoring of the form will be transparent to the end user. Usually the transit page will be so fast that the user doesn't see it.
This will also work with Ajax to some extent. Form fields inside an UpdatePanel will be re-populated by the form saver, but any callback from controls inside the UpdatePanel will be "replaced" by a full page postback from the transit page.
I have put together a small demo with the HttpModue and the transit page at MSDN Code Gallery: http://code.msdn.microsoft.com/formsaver.
Excellent idea, just one thing to note, in
ReplyDeleteFormSaverHttpModule.context_BeginRequest
it may be better to have
context.Response.Redirect("~/Timeouts/FormStateRestore.aspx?ReturnUrl=" + HttpUtility.UrlPathEncode(context.Request.RawUrl));
(UrlEncode replaced by UrlPathEncode to preseve slashes)