C# 禁用操作参数的xss验证(ValidateInput)

C# 禁用操作参数的xss验证(ValidateInput),c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,给定以下控制器和操作: public class CommentsController: AsyncController { [HttpPost, ValidateAntiForgeryToken, ValidateInput(false)] public ActionResult AddComment(string subject, string text) {..} } 当控制器接收到包含encodeURICompon

给定以下控制器和操作:

    public class CommentsController: AsyncController
    {
            [HttpPost, ValidateAntiForgeryToken, ValidateInput(false)]
            public ActionResult AddComment(string subject, string text) {..}
    }
当控制器接收到包含
encodeURIComponent(“”)数据的ajax请求时:

    Content-Type:application/x-www-form-urlencoded

    subject=hello&text=%3C%3E
主题设置正确,但文本始终为空。

为什么会这样?

请尝试将属性附加到输入属性,而不是使用
ValidateInput=false
。它只能附加到属性,所以您需要为输入参数创建POCO模型(顺便说一句,在您的情况下,最好有一个模型,例如包含其他验证属性)。例如:

public class CommentModel 
{
    public string Subject { get; set; }

    [AllowHtml]
    public string Text { get; set; }
}

[HttpPost, ValidateAntiForgeryToken]
public ActionResult AddComment(CommentModel model) ...

好吧,我假设我可以通过
返回到2.0 validationMode完全禁用验证,或者在客户端对字符串编码两次,然后在服务器上显式解码感谢您的响应。我尝试过这种方法,当
传递给
时,
文本
字段变为
。我重新创建了您的尝试。我使用了我的示例代码,并使用以下jQuery代码片段执行了一个AJAX请求:
$.post(“编辑/AddComment”,{subject:'hello',text:'',\uuuuuu RequestVerificationToken:'\uuuuu令牌})
-已通过页面上的
Html.AntiForgeryToken()
生成了反伪造令牌。事实上,这对我来说没有任何问题。