mercredi 3 juin 2009 11:50
sebmafate
[ASP.net] Erreur "Validation of viewstate MAC failed."
Il y a des jours où l'on passe trop de temps à s'arracher les cheveux !
Depuis quelques temps l'application sur laquelle je travaille remonte de manière aléatoire l'erreur suivante :
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Problème : cette application ne tourne pas sur un cluster !
Après quelques recherches sur l'Internet, il s'agirait d'un problème connu et il existerait un patch dans le SP1 du Framework 3.5. (Nous utilisons le Framework 2.0 pour notre application... oui, oui... je sais c'est vieux, mais l'immobilisme des grands groupes n'est pas une légende !)
J'ai aussi trouvé le pansement suivant qui consiste à surcharger la méthode Render des pages :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
protected override void Render(HtmlTextWriter writer) { StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString(); string[] aspnetFormelems = new string[5]; aspnetFormelems[0] = "__EVENTTARGET"; aspnetFormelems[1] = "__EVENTARGUMENT"; aspnetFormelems[2] = "__VIEWSTATE"; aspnetFormelems[3] = "__EVENTVALIDATION"; aspnetFormelems[4] = "__VIEWSTATEENCRYPTED"; foreach (string elem in aspnetFormelems) { //Response.Write("input type=""hidden"" name=""" & abc.ToString & """") int startPoint = html.IndexOf(string.Format(@"<input type=""hidden"" name=""{0}""", elem)); if (startPoint < 0) { continue; } //does __VIEWSTATE exist? int endPoint = html.IndexOf("/>", startPoint) + 2; string viewStateInput = html.Substring(startPoint, endPoint - startPoint); html = html.Remove(startPoint, endPoint - startPoint); int formStart = html.IndexOf("<form"); int endForm = html.IndexOf(">", formStart) + 1; if (endForm >= 0) { html = html.Insert(endForm, viewStateInput); } }
writer.Write(html); } |
Pour le moment, je n'ai pas eu le temps de tester la mise à jour du Framework... Par contre, le pansement ci-dessus donne des résultats satisfaisant : le bug surgit beaucoup moins souvent... mais il est toujours là !
Question : Rencontrez-vous ce problème ? Avez-vous des pistes ?
Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin :