如何阻止 ASP.Net 网站在向前/向后浏览项目分页列表时抛出 HttpUnhandledException 异常?

发布于 2024-07-09 20:19:29 字数 2724 浏览 7 评论 0 原文

我有一个 ASP.Net 托管网站,它将结果列表显示为带有结果分页的 DataGrid 或 ASP.Net Repeater。

如果通过按“上一个”/“下一个”选项卡快速滚动页面,有时会抛出 HttpUnhandledException 并呈现调试页面,而不是下一个结果列表。

调试屏幕如下:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
  at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
  at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
  at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  --- End of inner exception stack trace ---
  at System.Web.UI.Page.HandleError(Exception e)
  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.contacts_default_aspx.ProcessRequest(HttpContext context)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

我在以下位置找到了对此类错误的引用 http://blogs.msdn.com/amitsh/archive/2007/07/31/why-i-get-invalid-postback-or-callback-argument-errors.aspx

但建议的解决方案是设置为 false,但这似乎会造成安全漏洞。 这些评论推荐了几种替代方案,但似乎都相当复杂,因为它们需要向我的站点中使用的每个 DataGrid 或 Repeater 控件添加代码。

是否有更通用的解决方案可以在不牺牲安全性的情况下完成?

I have an ASP.Net hosted website which displays a list of results as a DataGrid or ASP.Net Repeater with paging of results.

If one scrolls quickly through the pages by pressing the Previous/Next tabs sometimes an HttpUnhandledException is thrown and the debug page rendered instead of the next listing of results.

The debug screen is as follows:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
  at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
  at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
  at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  --- End of inner exception stack trace ---
  at System.Web.UI.Page.HandleError(Exception e)
  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.contacts_default_aspx.ProcessRequest(HttpContext context)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I have found reference to such an error at
http://blogs.msdn.com/amitsh/archive/2007/07/31/why-i-get-invalid-postback-or-callback-argument-errors.aspx

but the solution recommended is to set to false, but this seems to create a security hole. The comments recommend several alternatives, but all seem rather complex since they require adding code to every single DataGrid or Repeater control used throughout my site.

Is there a more general solution that can be done without sacrificing security?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-07-16 20:19:29

回发时不会提供页面上的隐藏字段。
其中有几个字段,它们通常是 ASP.Net 提供的“魔力”所必需的。

在页面指令中,您可以将enableEventValidation = false放入页面指令中以将其关闭,但是这可能并不理想

您可以移动写入将元素移动到页面顶部的代码

最后,我相当确定这个确切的问题最近已在 Service Pack 或 3.5 中得到修复。

-- 编辑 --

我刚刚找到了设置: RenderAllHiddenFieldsAtTopOfForm

根据MSDN,以下版本支持:3.5 SP1、3.0 SP2、2.0 SP2

The hidden fields on your page are not being supplied on postback.
There are several of these fields, and they are usually required for the 'magic' that ASP.Net provides.

In your page directive you can put enableEventValidation=false in your page directive to turn it off, however that may not be desirable.

You can move the write code to move the elements to the top of the page

Finally, I am fairly certain this exact issue was recently fixed in a Service Pack or in 3.5.

-- EDIT --

I just found the setting: RenderAllHiddenFieldsAtTopOfForm

According to MSDN is supported in the following versions: 3.5 SP1, 3.0 SP2, 2.0 SP2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文