I have a requirement to implement a custom log-in block. As the block could be placed anywhere on the site, data messaging becomes more of a key feature. How will you render the control on a page, then post back to the page that calls it and trigger any Model State errors. My log-in block will need to have similar properties
As our block can be sat on any page, we need a way to send the users back to where they came from. To do this you will need to pass in the return Url, or, in Episerver, a better and more secure way is to pass a reference to the Episerver page. After the Login method is called, the controller will need to redirect the user to the page they made the original request from (in failure at least).
The problem with this is that if we do a re-direct we lose the Model state. To overcome this we can use TempData which is part of the MVC framework. I'm not a big fan of Temp data but as it only persists for the following page request it will do. I found a great way to pass values between two controllers called the PRG Pattern for Data Modification.
The basic premise is that you use attributes on the action methods you want to share the Temp data between. This practice is a lot more robust as you do not not need to manually store and retrieve information from Temp Data but you can use ActionFilters to do it automatically. The code to do this is here:
Now we have our action filters we can then decorate our actions as follows:
Also, one thing to note about the code is how the re-direct works. With Episerver, as long as you se the node, you can still use RedirectToAction. In this case I set a hidden field in the form and use the CurrentPage property to pass it in.
All the above code can be downloaded in a fully working website from my github account here. #ILoveEpiserver