2009-02-11 16 views
5

Acabo de actualizar a .NET 3.5 SP1 y mi página de ASP.NET MVC que alguna vez estuvo trabajando ha dejado de funcionar.Html.AntiForgeryToken() causa errores después de actualizar a .NET 3.5 SP1

Al intentar cargar una página me sale el siguiente YSOD

[CryptographicException: Padding is invalid and cannot be removed.] 
    System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) +7596702 
    System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) +208 
    System.Security.Cryptography.CryptoStream.FlushFinalBlock() +33 
    System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSymAlgo) +225 
    System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +195 

[ViewStateException: Invalid viewstate. 
    Client IP: 127.0.0.1 
    Port: 
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729) 
    ViewState: hC6BC8KsuD/yoy2iG74bUZ8TYhGfuDDeIjh9fg/L18yr/E+1Nk/pjS5gyn9O+2jY 
    Referer: http://localhost:1092/admin/product 
    Path: /admin.aspx/product/edit/4193] 

[HttpException (0x80004005): 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.] 
    System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +106 
    System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +14 
    System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +242 
    System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4 
    System.Web.Mvc.AntiForgeryTokenSerializer.Deserialize(String serializedToken) +73 

Si quito la línea

<%= Html.AntiForgeryToken() %> 

Todo funciona de nuevo, cualquier idea lo que podría estar causando esto? Hubiera esperado más suerte para encontrar una solución si esto era un problema MVC o .NET, así que supongo que tiene algo que ver con mi configuración.

He intentado volver a instalar el marco MVC para ver si era porque lo instalé antes de SP1 pero sigo teniendo el mismo problema. La búsqueda de Google y SO no ha dado lugar a conclusiones firmes.

Respuesta

15

Doh, acabo de resolverlo.

Borré la memoria caché y las cookies de mi navegador y todo vuelve a funcionar bien.

+0

Gracias! Esto realmente me ayudó, se estaba volviendo loco. – jesperlind

+0

Me ahorró 2 horas de investigación. Gracias –

+3

Del mismo modo. ¿Por qué esto soluciona el problema? ¿Y por qué hubo un problema en primer lugar? –

3

La eliminación de la memoria caché del navegador no es una opción si el sitio ya está implementado y usted está realizando tareas de mantenimiento, incluida una actualización del conjunto ASP.NET MVC. Aquí está la solución utilicé:

@Html.AntiForgeryTokenReset() @* use this instead*@ 

aquí es el método de extensión

public static MvcHtmlString AntiForgeryTokenReset(this HtmlHelper htmlHelper) 
{ 
    try 
    { 
     return htmlHelper.AntiForgeryToken(); 
    } catch (Exception ex) 
    { 
     var request = HttpContext.Current.Request; 
     request.Cookies.Clear(); 
     return htmlHelper.AntiForgeryToken(); 
    } 
} 
+0

Solución interresante. Pensé en usar esto, pero como la cookie es una cookie de sesión, desaparecerá para los visitantes cuando cierren el navegador. Como de todos modos he agregado una clave de máquina para web.config, no será un problema en futuras actualizaciones. – Guffa

Cuestiones relacionadas