ASP.NET中的 Base-64 字符串存在无效字符

3
我有一个 C# 手机站点,但是一些移动客户端存在问题。我在下面发布了一个跟踪,但基本上是手机的浏览器或连接到互联网的 WAP 网关会对表单中的 ViewState 隐藏输入进行 URL 编码。

/wEPDwULLTExNTMyOTcwOTBkGAEFBlBtTGlzdA9nZA==

变成了

%2FwEPDwULLTExNTMyOTcwOTBkGAEFBlBtTGlzdA9nZA%3D%3D

因此 ViewState 失败。
是否有任何方法可以在处理之前覆盖并解码 ViewState 信息?
System.Web.HttpException: 此页的状态信息无效,可能已损坏。 ---> System.Web.UI.ViewStateException: 无效的 ViewState。
客户端 IP:65.91.116.34
端口:37172
User-Agent:SCH-R430 UP.Browser/6.2.3.8 (GUI) MMP/2.0
ViewState:%2FwEPDwULLTExNTMyOTcwOTBkGAEFBlBtTGlzdA9nZA%3D%3D
Referer:
路径:/mobile/Inbox.aspx ---> System.FormatException: Base-64 字符串中有无效字符。
   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()
   --- 内部异常堆栈跟踪的结尾 ---
   --- 内部异常堆栈跟踪的结尾 ---
   at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
   at System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState)
   at System.Web.UI.HiddenFieldPageStatePersister.Load()
   at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
   at System.Web.UI.Page.LoadAllState()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.mobile_inbox_aspx.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

(删除了我的答案,因为我没有注意到“视图状态”这一部分) - Marc Gravell
2个回答

3
你可以实现一个自定义的ViewStatePersister对象来处理这个问题。你可能想要从HiddenFieldPageStatePersister派生它。参考this article,该文章展示了如何在ViewState上实现压缩,但与你需要做的非常相似。
有一点点hackiness:你需要使用反射来设置StateFormatter基类的一个字段,这个字段与MSDN文档所说的不同,没有标记为虚拟,因此不能通过重写覆盖它,需要使用反射。

-1

使用以下解决方案并检查是否有效。它对我有效。将此代码添加到您的引起问题的asp.net代码后面。下面的代码是vb.net。

Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        Return Session("_ViewState")
    End Function
Protected Overrides Sub SavePageStateToPersistenceMedium(viewState As Object)
        Session("_ViewState") = viewState
    End Sub

你需要提供问题所要求的编程语言的代码,而不是你自己选择的编程语言。 - AStopher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接