views:

13196

answers:

3

I have an ASP.NET 2.0 application that is working fine in our local environment. When published to a test server, we're getting intermittent errors on the server.

Here's the most common:

Padding is invalid and cannot be removed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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) +1545747
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) +257
System.Security.Cryptography.CryptoStream.FlushFinalBlock() +30 System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Boolean useValidationSymAlgo) +164
System.Web.UI.Page.DecryptString(String s) +83
System.Web.Handlers.AssemblyResourceLoader.System.Web.IHttpHandler.ProcessRequest(HttpContext context) +148
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

This happens on a request to a specific WebResource.axd request.

The other error that we're seeing is this:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

This error happens occasionally while posting a form.

Now before you jump in and tell me the obvious (usual) answer to this error, consider this:

  • The application is deployed to Windows Server 2003 on IIS6
  • It is NOT on a web farm. Only one webserver is in use here.
  • The application pool identity is a custom service account, and I did run aspnet_regiss -ga <username> on the server. No effect.
+1  A: 

This problem appears when a post is generated before the page is fully loaded in the browser. Have a look at this question.

Eduardo Campañó
that's what I was thinking for the secondary issue, however it happens more often than is comfortable.This doesn't explain the first error, which is far more serious in our case.
Ben Scheirman
are you using HTTP or HTTPS?
Eduardo Campañó
All HTTP. []
Ben Scheirman
I think the WebResource.axd uses some crypted parameter to validate the request using Rijndael algorithm. Maybe you can look at the IIS logs to see if you're having some pool recycling that's making your request not to validate.
Eduardo Campañó
I'll look into this... thanks.
Ben Scheirman
A: 

Ben,

For your first problem, I found this that seems to be a bit more to the point of what you are seeing in that the problem is sporadically occurring. You should be able to find a full explanation of this at http://www.codeproject.com/KB/security/Cryptor.aspx#aes.

What you really need to do is set RijndaelAlg.Padding to PaddingMode.ISO10126, PaddingMode.PKCS7, or PaddingMode.ANSIX923. Any one of these 3 values should work, provided that you use the same value when encrypting and decrypting. Other values will work with some data, but not with all data. The above URL explains why.

What I don't understand is the reason Microsoft provides options that sometimes don't work, or at least why they don't default to a reliable option.

JohnL
I'm not doing any compression or encryption on my own, which is why this is confusing. This just happens on a requests to various WebResource.axd URLs.
Ben Scheirman
Are you using any 3rd party components that are?
JohnL
+18  A: 

The error is because your appdomain was recycled/restarted. When that happens the application and the machine key is set to auto, it changes. That affects the decryption of the info in the url of the resources urls (.axd). Setting up a fixed machine key will prevent it from ever happening again.

Please check this for more info on a similar case (the explanation is with an issue with viewstate validation, but the cause is the same one): http://www.developmentnow.com/blog/InvalidViewstate+Or+Unable+To+Validate+Data+Error.aspx

I also had been wondering about it for quite a while. After I saw this question it got me on it again: http://stackoverflow.com/questions/617782/is-this-an-attempt-to-break-my-asp-net-sites-security/617940#617940 ... which I just answered with very much the same. I had the feeling it was around some restart of something, because when we published something that recycled the application the error showed in the log, but I didn't have any other source stating its relation (today I found that case on invalidviewstate because of the machinekey change :))

Ps. above finally explains it on single server :)

eglasius
I'll do some investigation, but I don't think this is the problem. If a server has a machine key specified in machine.config, then it should never change. Thanks for the tips though, I'll look into it.
Ben Scheirman
you are correct, if you specify the machine key it won't change (that's the fix but almost every source only apply/explains it to multi server), just thought you was using auto for the key as you made emphasis on "It is NOT on a web farm. Only one webserver is in use here." ...
eglasius
looking back on this problem, this was most likely scenario
Ben Scheirman
I just wanted to add that once you do all of the things recommended by Freddy (great answer, by they way!) - don't forget to clear your cookies and cache. For me, the problem was caused by adding the ASP.NET MVC AntiForgeryToken... this apparently adds a cookie, and you'll keep seeing the same error even if you follow all of Freddy's instructions. Unitl... you clear your cookies and cache!
leftend
Thanks a lot! In my case, I had the a copy of the same `.csproj` in two different solutions. When I opened both, one took over the port of the other. Since the cookies remained -- boom! different 'machine' keys. Resolution: delete cookies.
mnemosyn