首页 文章

从客户端检测到一个潜在危险的Request.Form值:哪个解决方案

提问于
浏览
0

我有一个使用aspx页面的Web应用程序 . 首先,我想使用 Server.HtmlEncode(value) ,在 LabelledTextBox 中显示值

public interface ILabelledControl
{
    bool ReadOnly { get; set; }
}

[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class LabelledTextBox : TextBox, ILabelledControl
{
    //public Unit EditableWidth { get; set; }
    public Unit ReadOnlyWidth { get; set; }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        if (this.ReadOnly)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            foreach (string att in this.Attributes.Keys)
                lbl.Attributes.Add(att, this.Attributes[att]);
            lbl.Text = this.Text;
            lbl.ForeColor = ForeColor;
            //lbl.Width = this.Width;
            if (ReadOnlyWidth != null)
                lbl.Width = ReadOnlyWidth;
            lbl.CssClass = CssClass;
            lbl.ID = this.ID;
            lbl.RenderControl(writer);
        }
        else
        {

            base.Render(writer);
        }
    }
}

显示值 <script>alert("hello")</script> 但脚本已执行 .

之后,我想尝试另一种处理异常的解决方案

A potentially dangerous Request.Form value was detected from the client

保持在包含表单的同一页面上,并在顶部显示一条错误消息,其中包含一般消息,例如“请确保所有输入都不包含'<'或'>'等字符

Solution 1 :我做错了什么?

Solution 2 : 如何处理此异常并使用填写的表单保持在同一页面上

General : 哪种解决方案最好?

谢谢 !

1 回答

  • 0

    从客户端检测到一个潜在危险的Request.Form值

    您会看到此服务器异常,因为用户在 Textbox control中输入HTML标记(例如<>),并提交表单 .

    没有什么可以阻止它在服务器端 . 但是,您可以创建客户端验证脚本,并警告用户或删除标记 .

    例如,

    <asp:RegularExpressionValidator 
         ID="RegularExpressionValidator1" runat="server"    
         ControlToValidate="MyTextBox"
         ErrorMessage="Please do not enter HTML tags." 
         ValidationExpression="<(.|\n)*?>">
    </asp:RegularExpressionValidator>
    

    显示值alert(“hello”)但脚本已执行

    这与上述情况相反 . 服务器将脚本标记呈现给浏览器 .

    为了防止这种情况,正如你所说,你希望在重新发送之前使用HttpServerUtility.HtmlEncode对字符串进行编码 .

    lbl.Text = Server.HtmlEncode(this.Text);
    

相关问题