每当用户在我的web应用程序中的页面中发布包含<或>的内容时,我都会引发此异常。

我不想因为有人在文本框中输入了字符而引发异常或使整个web应用程序崩溃,但我正在寻找一种优雅的方式来处理这一问题。

捕获异常并显示

出现错误,请返回并重新键入整个表单,但这次请不要使用<

我觉得不够专业。

禁用后验证(validateRequest=“false”)肯定可以避免此错误,但这会使页面容易受到许多攻击。

理想情况下:当发生包含HTML限制字符的回发时,表单集合中的回发值将自动进行HTML编码。因此,我的文本框的.Text属性将是&lt;html&gt;

有没有办法让我从处理者那里做到这一点?


当前回答

没有一个答案对我有效。然后我发现,如果我删除了以下代码,我可以让它发挥作用:

//Register action filter via Autofac rather than GlobalFilters to allow dependency injection
builder.RegisterFilterProvider();
builder.RegisterType<OfflineActionFilter>()
    .AsActionFilterFor<Controller>()
    .InstancePerLifetimeScope();

我只能得出结论,Autofac的RegisterFilterProvider中的某些内容破坏或覆盖validateRequest属性

其他回答

如果您使用的是ASP.NET MVC,则此错误有一种不同的解决方案:

ASP.NET MVC–pages validateRequest=false不起作用?为什么ValidateInput(False)不工作?ASP.NET MVC RC1,VALIDATEINPUT,一个潜在的危险请求和陷阱

C#示例:

[HttpPost, ValidateInput(false)]
public ActionResult Edit(FormCollection collection)
{
    // ...
}

Visual Basic示例:

<AcceptVerbs(HttpVerbs.Post), ValidateInput(False)> _
Function Edit(ByVal collection As FormCollection) As ActionResult
    ...
End Function

最后但同样重要的是,请注意ASP.NET数据绑定控件在数据绑定期间自动编码值。这将更改ItemTemplate中包含的所有ASP.NET控件(TextBox、Label等)的默认行为。以下示例演示了(ValidateRequest设置为false):

aspx

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication17._Default" %> <html> <body>
    <form runat="server">
        <asp:FormView ID="FormView1" runat="server" ItemType="WebApplication17.S" SelectMethod="FormView1_GetItem">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                <asp:Label ID="Label1" runat="server" Text="<%#: Item.Text %>"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server" Text="<%#: Item.Text %>"></asp:TextBox>
            </ItemTemplate>
        </asp:FormView>
    </form> 

代码隐藏

public partial class _Default : Page
{
    S s = new S();

    protected void Button1_Click(object sender, EventArgs e)
    {
        s.Text = ((TextBox)FormView1.FindControl("TextBox1")).Text;
        FormView1.DataBind();
    }

    public S FormView1_GetItem(int? id)
    {
        return s;
    }
}

public class S
{
    public string Text { get; set; }
}

案例提交值:&#39;

标签1.文本值:&#39;

TextBox2.文本值:&amp#39;

案例提交值:<script>alert('attack!')</脚本>

标签1.文本值:<script>alert('attack!')</脚本>

TextBox2.文本值:&lt;脚本&gt;警报(&#39;攻击!&#39;)&lt/脚本&gt;

我有一个Web表单应用程序在文本框注释字段中遇到了这个问题,用户有时会在其中粘贴电子邮件文本,电子邮件标题信息中的“<”和“>”字符会在其中爬行并引发此异常。

我从另一个角度解决了这个问题。。。我已经在使用Ajax控件工具包,所以我能够使用FilteredTextBoxExtender来防止这两个字符进入文本框。用户复制粘贴文本将得到他们期望的结果,减去这些字符。

<asp:TextBox ID="CommentsTextBox" runat="server" TextMode="MultiLine"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="CommentsTextBox" filterMode="InvalidChars" InvalidChars="<>" />

我看到有很多关于这个的文章。。。我没有看到这一点。自.NET Framework 4.5以来,此功能一直可用

控件的ValidateRequestMode设置是一个很好的选项。这样,页面上的其他控件仍受保护。不需要更改web.config。

 protected void Page_Load(object sender, EventArgs e)
 {
     txtMachKey.ValidateRequestMode = ValidateRequestMode.Disabled;
 }

您可以对文本框内容进行HTML编码,但不幸的是,这并不能阻止异常的发生。根据我的经验,没有办法,您必须禁用页面验证。你这样做是在说:“我会小心的,我保证。”