控制文本中的 ASP.NET 和 HTML/XML
我有许多必须能够接受 XML 输入的 Web 表单(当然,包含 <
和 >
字符,我认为这是导致问题的原因)。但是,目前每当我单击提交按钮并且其中一个表单字段包含此类输入时,就会引发 Sys.WebForms.PageRequestManagerServerErrorException
异常,代码为 500。如果输入没有 <
和 >
一切都会顺利。我该如何解决这个问题?
I have a number of webforms which must be able to accept XML input (containing, of course, the <
and >
characters, which I believe is what is causing problems). However, presently whenever I click a submit button and one of the form fields contains this kind of input a Sys.WebForms.PageRequestManagerServerErrorException
exception with code 500 is thrown. If the input has no <
and >
everything goes smooth. How can I work around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对关闭页面验证持谨慎态度。请参阅 HTML 对文本进行编码。
I would be cautious about turning off Page validation. Look at HTML Encoding the text instead.
默认情况下启用请求验证。您可以使用 web.config 将其关闭所有页面:
或者仅在该页面上将其添加到指令中:
Request validation is enabled by default. You can turn it off all pages using web.config:
or just on that page by adding it to the directive:
如果您仍然希望页面请求验证正常运行(即不将其设置为 false),您可以随时用字符替换 <; & >符号并从服务器重新替换它们。
If you still want the page request validation to function properly (i.e. w/o setting it false) you can always character substitute the < & > symbols and re-substitute them back from the server.
每当您处理用户的 HTML/XML 输入时,您都必须非常小心,并确保彻底清理输入。
不同的 Web 控件默认实现(或不实现)HtmlEncoding。看一下 哪些 ASP.NET 控件自动编码? 获取详细列表。
我还建议您查看 Microsoft 反交叉网站脚本库。
您可以编辑您的问题并发布一些代码(也许还有导致错误的示例输入)?
<
和>
可能是原因,但也可能是转移注意力。Anytime you're dealing with HTML/XML input by a user you have to be extremely careful and make you sure you sanitize the input thoroughly.
Different web controls implement (or don't) HtmlEncoding by default. Take a look at Which ASP.NET Controls Automatically Encodes? for a detailed list.
I'd also recommend looking at the Microsoft Anti-Cross Site Scripting Library.
Can you edit your question and post some of the code (and maybe sample input that causes the error)? The
<
and>
might be the cause, but it might be a red herring as well.