Padding is invalid and cannot be removed 解决方法

   最近,客户浏览公司开发的网站时,老是出现 Padding is invalid and cannot be removed 的错误

具体信息如下:

Server Error in '/gzdisable' Application.
--------------------------------------------------------------------------------

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) +7594654
   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.Security.FormsAuthentication.Decrypt(String encryptedTicket) +246
   ZSoft.Web.Engine.GetCookie(HttpContext context, String cookieName) +140
   ZSoft.Web.Engine.SetContext(String cookieName) +130
   ZSoft.Web.Engine.SetContext() +55
   ZSoft.Web.HttpModule.BeginRequest(Object sender, EventArgs e) +45
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 

 经查询,发现是因为网站用了负载平衡(Web Farm) 而且启用视图状态加密而导致的。

 解决方法有两种:

1,可以同时在各台负载平衡的网站web.config的<system.web>节点加上这个节点

<machineKey validationKey="0BE61B38B9836B541C45728ADB9D93A6FD819169DBB6AD20078A70F474650CC0295C69131E083A6B3762C457BBAF3E66E18F294FDA434B9DD6758631A90A2E20" decryptionKey="B80CC12266B36CCF35EF0708DB5854EDA3BBEBA1A7C89A4E" validation="SHA1"/>

 这样可以保持对试图状态的加密,又不会出现错误

2, 可在web.config 作如下配置,目的是关闭视图状态加密
<system.web>

<pages enableViewStateMac="false" />

</system.web>

 也可在每个页面前面单独配置:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyPage.aspx.vb"
 Inherits="MyAssembly.MyPage" enableViewStateMac="False" %> 

 在此引用MSDN对EnableViewStateMac的解释:

EnableViewStateMac

指示当页从客户端回发时,ASP.NET 是否应该对页的视图状态运行计算机身份验证检查 (MAC)。如果应该对视图状态运行 MAC 检查,则为 true;否则为 false。默认值为 true。

注意:

视图状态 MAC 是隐藏变量的加密版本,当某页被发送到浏览器时,该页的视图状态永久保持到该变量中。当您将此属性设置为 true 时,将检查加密的视图状态以验证它在客户端未被篡改。注意,将此属性设置为 true 会影响性能,因为在页的每次往返行程中,都必须加密和解密变量值。


 其实对于一般的网站,没有必要对ViewState加密,建议用第二种解决方案。

 参考:

1,Web Farm And Web Garden

2,Padding is invalid and cannot be removed

原文地址:https://www.cnblogs.com/lostpaddle/p/1598993.html