Validation of viewstate MAC failed. Triggered by URL-rewriting
There has been a lot of posting activity around the ASP.Net error "Validation of viewstate MAC failed". In particular, you might like to view the (now locked) forum thread over at http://forums.asp.net/t/955145.aspx?PageIndex=1
This gave me some help in finding out why I was getting the same problem on a recent project. In my case, the project was targeting .Net 3.5 and the issue was only occurring with any post-back controls from a specific page (I was using a LinkButton). Hyperlink controls were OK. In brief, it was the form's action property that was triggering the error.
This particular project uses an URL-rewriting process to deliver a human- and search-engine friendly address for job vancancies in our company. The content of the various pages that the friendly URLs represent is all actually handled by one single page. So the result was that the form action was by default being set to the real underlying page plus it's querystring parameters (the ID of the vacancy record), which clearly didn't match the displayed friendly URL and so the event validation decrypt fails.
The solution was therefore pretty trivial once discovered! Override the form action value to use the value from Request.RawUrl instead. This override process is put inside the Page_Load handler and only needs to run on first load.
My sample here outlines the explicit steps for clarity. I am trimming the leading path from RawUrl as on the development PC we would get the project sub-folder, and on our production system the page is in the root of the site, so no additional path is required.
If Not Page.IsPostBack
'Get the displayed re-written URL the user sees.
Dim actionPath As String = Me.Request.RawUrl
Dim pos1, pos2 As Integer
'Find the trim points, fore and aft
pos1 = actionPath.LastIndexOf("/") + 1
pos2 = actionPath.Length - pos1
'Strip out any preceding folder pathing.
actionPath = actionPath.Substring(pos1, pos2)
'Then assign the now-matching friendly URL address as the form's action value.
Me.Form.Action = actionPath
'Any other processing...etc...
Once that's in place, all post-back controls (such as my LinkButton) work as expected.