是什么导致了“Base-64 字符数组的长度无效”?
我在这里没什么可说的。我无法在本地重现此问题,但是当用户收到错误时,我会收到自动电子邮件异常通知:
Invalid length for a Base-64 char array.
at System.Convert.FromBase64String(String s)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)
at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)
at System.Web.UI.HiddenFieldPageStatePersister.Load()
我倾向于认为分配给视图状态的数据存在问题。 例如:
List<int> SelectedActionIDList = GetSelectedActionIDList();
ViewState["_SelectedActionIDList"] = SelectedActionIDList;
如果无法在本地重现错误,就很难猜测错误的来源。
如果有人有过此错误的经验,我真的很想知道你发现了什么。
I have very little to go on here. I can't reproduce this locally, but when users get the error I get an automatic email exception notification:
Invalid length for a Base-64 char array.
at System.Convert.FromBase64String(String s)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)
at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)
at System.Web.UI.HiddenFieldPageStatePersister.Load()
I'm inclined to think there is a problem with data that is being assigned to viewstate.
For example:
List<int> SelectedActionIDList = GetSelectedActionIDList();
ViewState["_SelectedActionIDList"] = SelectedActionIDList;
It's difficult to guess the source of the error without being able to reproduce the error locally.
If anyone has had any experience with this error, I would really like to know what you found out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
urlDecode 处理文本后,它将所有 '+' 字符替换为 ' ' ...因此出现错误。您应该简单地调用此语句以使其再次兼容 Base 64:
After urlDecode processes the text, it replaces all '+' chars with ' ' ... thus the error. You should simply call this statement to make it base 64 compatible again:
我见过这个错误是由良好大小的视图状态和过度激进的内容过滤设备/防火墙相结合引起的(特别是在与 K-12 教育机构打交道时)。
我们通过将 Viewstate 存储在 SQL Server 中来解决这个问题。在走这条路之前,我建议尝试限制视图状态的使用,不要在其中存储任何大的内容,并为所有不需要它的控件关闭它。
在 SQL Server 中存储 ViewState 的参考:
MSDN - PageStatePersister 概述
ASP 联盟 - 在 SQL Server 中存储视图状态的简单方法
代码项目 - ViewState 提供程序模型
I've seen this error caused by the combination of good sized viewstate and over aggressive content-filtering devices/firewalls (especially when dealing with K-12 Educational institutions).
We worked around it by storing Viewstate in SQL Server. Before going that route, I would recommend trying to limit your use of viewstate by not storing anything large in it and turning it off for all controls which do not need it.
References for storing ViewState in SQL Server:
MSDN - Overview of PageStatePersister
ASP Alliance - Simple method to store viewstate in SQL Server
Code Project - ViewState Provider Model
我的猜测是编码或解码过于频繁 - 或者您的文本包含多行。Base64
字符串的长度必须是 4 个字符的倍数 - 每 4 个字符代表 3 个字节的输入数据。不知何故,ASP.NET 传回的视图状态数据已损坏 - 长度不是 4 的倍数。
发生这种情况时您是否记录用户代理?我想知道它是否是某个地方行为不良的浏览器...另一种可能性是有一个代理在做一些顽皮的事情。同样尝试记录请求的内容长度,以便您可以查看它是否只发生在大型请求中。
My guess is that something is either encoding or decoding too often - or that you've got text with multiple lines in.
Base64 strings have to be a multiple of 4 characters in length - every 4 characters represents 3 bytes of input data. Somehow, the view state data being passed back by ASP.NET is corrupted - the length isn't a multiple of 4.
Do you log the user agent when this occurs? I wonder whether it's a badly-behaved browser somewhere... another possibility is that there's a proxy doing naughty things. Likewise try to log the content length of the request, so you can see whether it only happens for large requests.
试试这个:
Try this:
其中
qs
是任何 Base64 编码的字符串where
qs
is any base64 encoded string正如其他人提到的,当某些防火墙和代理阻止访问包含大量 ViewState 数据的页面时,可能会导致这种情况。
ASP.NET 2.0 引入了 ViewState 分块机制,它将 ViewState 分解为可管理的块,允许 ViewState 毫无问题地通过代理/防火墙。
要启用此功能,只需将以下行添加到您的 web.config 文件中。
这不应该用作减小 ViewState 大小的替代方法,但它可以有效防止因激进代理等导致的“Base-64 字符数组的长度无效”错误。
As others have mentioned this can be caused when some firewalls and proxies prevent access to pages containing a large amount of ViewState data.
ASP.NET 2.0 introduced the ViewState Chunking mechanism which breaks the ViewState up into manageable chunks, allowing the ViewState to pass through the proxy / firewall without issue.
To enable this feature simply add the following line to your web.config file.
This should not be used as an alternative to reducing your ViewState size but it can be an effective backstop against the "Invalid length for a Base-64 char array" error resulting from aggressive proxies and the like.
遗憾的是,这不是答案。在遇到间歇性错误一段时间并最终恼火地尝试修复它之后,我还没有找到解决方法。然而,我已经确定了重现我的问题的方法,这可能对其他人有帮助。
就我而言,这完全是本地主机问题,在我的开发机器上也有应用程序的数据库。这是我用 VS2005 编辑的 .NET 2.0 应用程序。 Win7 64位机器还安装了VS2008和.NET 3.5。
以下是各种表单会产生错误的原因:
一两分钟的延迟“等待 localhost”,然后浏览器“连接已重置”,以及
global.asax
的应用程序错误陷阱日志:在这种情况下,它不是视图状态,但与页面和/或视图状态缓存有关的东西似乎让我烦恼。在参数
Web.config
enableEventValidation="false"
和viewStateEncryption="Never"
> 没有改变行为。将maxPageStateFieldLength
设置为适度的值也没有。This isn't an answer, sadly. After running into the intermittent error for some time and finally being annoyed enough to try to fix it, I have yet to find a fix. I have, however, determined a recipe for reproducing my problem, which might help others.
In my case it is SOLELY a localhost problem, on my dev machine that also has the app's DB. It's a .NET 2.0 app I'm editing with VS2005. The Win7 64 bit machine also has VS2008 and .NET 3.5 installed.
Here's what will generate the error, from a variety of forms:
A minute or two delay "waiting for localhost" and then "Connection was reset" by the browser, and
global.asax
's application error trap logs:In this case, it is not the SIZE of the viewstate, but something to do with page and/or viewstate caching that seems to be biting me. Setting
<pages>
parametersenableEventValidation="false"
, andviewStateEncryption="Never"
in theWeb.config
did not change the behavior. Neither did setting themaxPageStateFieldLength
to something modest.查看您的 HttpHandler。在我实施压缩工具(来自 Telerik 的 RadCompression)后的几个月里,我注意到一些奇怪且完全随机的错误。我注意到以下错误:
System.Web.HttpException:无法验证数据。
System.Web.HttpException:客户端已断开连接。---> System.Web.UI.ViewStateException:无效的视图状态。
和
System.FormatException:Base-64 字符数组的长度无效。
System.Web.HttpException:客户端已断开连接。 ---> System.Web.UI.ViewStateException:无效的视图状态。
我在我的博客上写了这个。
Take a look at your HttpHandlers. I've been noticing some weird and completely random errors over the past few months after I implemented a compression tool (RadCompression from Telerik). I was noticing errors like:
System.Web.HttpException: Unable to validate data.
System.Web.HttpException: The client disconnected.---> System.Web.UI.ViewStateException: Invalid viewstate.
and
System.FormatException: Invalid length for a Base-64 char array.
System.Web.HttpException: The client disconnected. ---> System.Web.UI.ViewStateException: Invalid viewstate.
I wrote about this on my blog.
这是因为一个巨大的视图状态,就我而言,我很幸运,因为我没有使用视图状态。我刚刚在表单标记上添加了
enableviewstate="false"
,视图状态从 35k 字符增加到 100 个字符This is because of a huge view state, In my case I got lucky since I was not using the viewstate. I just added
enableviewstate="false"
on the form tag and view state went from 35k to 100 chars在使用 SqlMembershipProvider 对 Membership.ValidateUser 进行初始测试期间,我使用了哈希 (SHA1) 算法和盐,如果我将盐长度更改为不能被四整除的长度,则会收到此错误。
我没有尝试上面的任何修复,但如果盐被改变,这可能会帮助某人查明这是这个特定错误的根源。
During initial testing for Membership.ValidateUser with a SqlMembershipProvider, I use a hash (SHA1) algorithm combined with a salt, and, if I changed the salt length to a length not divisible by four, I received this error.
I have not tried any of the fixes above, but if the salt is being altered, this may help someone pinpoint that as the source of this particular error.
正如 Jon Skeet 所说,字符串必须是 4 字节的倍数。但我仍然收到错误。
至少它在调试模式下被删除了。在
Convert.FromBase64String()
上放置一个断点,然后单步执行代码。奇迹般的是,错误对我来说消失了:)它可能与视图状态和其他人报告的类似其他问题有关。As Jon Skeet said, the string must be multiple of 4 bytes. But I was still getting the error.
At least it got removed in debug mode. Put a break point on
Convert.FromBase64String()
then step through the code. Miraculously, the error disappeared for me :) It is probably related to View states and similar other issues as others have reported.除了@jalchr的解决方案对我有帮助之外,我发现在调用
ATL::Base64Encode
时从 C++ 应用程序对传递到 ASP.NET Web 服务的内容进行编码,您还需要其他东西。除了@jalchr 的解决方案之外,您还需要确保不要在
ATL::Base64Encode
上使用ATL_BASE64_FLAG_NOPAD
标志:In addition to @jalchr's solution that helped me, I found that when calling
ATL::Base64Encode
from a c++ application to encode the content you pass to an ASP.NET webservice, you need something else, too. In addition tofrom @jalchr's solution, you also need to ensure that you do not use the
ATL_BASE64_FLAG_NOPAD
flag onATL::Base64Encode
: