【问题标题】:Passing Html string to div innerhtml and its causing errors将 Html 字符串传递给 div innerhtml 及其导致错误
【发布时间】:2014-02-27 04:15:15
【问题描述】:

当我尝试将它放在这样的文本框中时遇到错误,

tbGameTitle.Text = "<iframe id = 'ForIframe' src='http://e.gamesalad.com/play/117208' allowTransparency='true' scrolling='no'></iframe>";

当我点击我的按钮时

myThing.InnerHtml = tbGameTitle.Text;

它会抛出这个错误

A potentially dangerous Request.Form value was detected from the client (tbGameTitle="<iframe id = 'ForIfr..."). 

如果我在 pageload 事件中有这个负载,那么它很好。但是,只要我在文本框中输入它并单击我的按钮,它就会引发该错误。我从很久以前就让它在另一个项目中工作,它从来没有抛出这个错误。

【问题讨论】:

    标签: c# html asp.net webforms


    【解决方案1】:

    ASP.Net 表单内容在提交时会检查是否存在危险内容(HTML 和 javascript 等内容被标记为潜在危险并被拒绝)。有几种方法可以提交此内容,在将文本发送到服务器之前对文本进行 HTML 编码,或者使用页面顶部的标签禁用请求验证(可能不安全!):

    <@ Page validateRequest="false" %>
    

    有关请求验证的更多信息,请访问this link

    【讨论】:

    • 这行得通,我唯一希望的是,当我添加 requiredfieldvalidation 时,它会导致任何冲突
    • 如果您关闭了请求验证,请记得手动检查传递的数据服务器端是否存在恶意输入!
    【解决方案2】:

    您需要转义“XSS

    这个post 一定很有帮助。


    您想要从该页面获得的主要代码是:

    // The event to escape the data and store in our HiddenField
    jQuery('.allow_html textarea').blur(function () {
        jQuery(jQuery(this).parent()).find('input[type="hidden"]').val(escape(jQuery(this).val()));
    });
    
    // The code to unescape the code and set it in our textbox
    jQuery('.allow_html textarea').each(function(idx, item) {
        var value = jQuery(jQuery(item).parent()).find('input[type="hidden"]').val();
        jQuery(item).val(unescape(value));
    });
    

    这将在输入中转义 HTML 代码。

    并且,在服务器端,您需要取消转义:

    // encode the data
    HtmlCodeHiddenField.Value = Uri.EscapeDataString(EscapedHtml);
    // decode the data
    string myHtml = Uri.UnescapeDataString(HtmlCodeHiddenField.Value);
    

    【讨论】:

    • 您能否详细说明您的答案?发布大部分仅链接的答案通常会被删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多