Html.AntiForgeryToken () causes errors after upgrading to .NET 3.5 SP1

I just upgraded to .NET 3.5 SP1, and my once-working ASP.NET MVC page stopped working.

When I try to load the page, I get the following 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

If I delete the line

<%= Html.AntiForgeryToken() %>

Everything works again, any ideas what could be causing this? I expected more success in finding a solution if this was an MVC or .NET problem, so I assume this is due to my configuration.

I tried reinstalling the MVC structure to make sure it was because I installed before SP1, but I still get the same problem. The Google and SO search did not lead to any firm conclusions.

+5
source share
3

, .

cookie, .

+15

, , , ASP.NET MVC. , :

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

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();
    }
}
+3

All Articles