c# - 为 Controller Action 参数创建过滤属性抗XSS攻击

标签 c# asp.net asp.net-mvc xss

我有一个像这样的简单 Controller :

[CustomFilter()]
public ActionResult Index( int? page ) {
    return View();
}


public class CustomFilterAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting( ActionExecutingContext filterContext ) {
      base.OnActionExecuting( filterContext );
}

public override void OnActionExecuted( ActionExecutedContext filterContext ) {
      base.OnActionExecuted( filterContext );
   }
}

在主页 (ASPX) 中,如果我输入 http://localhost/home/index?page=<script> (我猜是 XSS 的一个例子)而不是 http://localhost/home/index?page=7然后会出现红色页面(因为在 Visual Studio 中我已经安装了 AntiXSS 插件)。

如何为此类攻击创建自定义过滤器并返回上一页而不显示红色页面(有错误)? 或者不可能?

最佳答案

不,永远不会调用过滤器。你可以在 Application_Error

中做类似的事情

检查这个问题:ASP.NET MVC app custom error pages not displaying in shared hosting environment

看问题和答案。

关于c# - 为 Controller Action 参数创建过滤属性抗XSS攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061831/

相关文章:

c# - 如何正确反序列化 Json DateTimeOffset?

c# - 分配多个 JsonProperties?

c# - PE32+和PE32有什么区别?

asp.net-mvc - ASP.NET MVC View 可以在不同的项目中重复使用吗?

c# - 从数据库更新 Entity Framework 模型,无法识别自定义属性

c# - 如何在asp.net中使用Page_Init

javascript - 强制 AutoCompleteExtender 下拉菜单通过 Javascript 重新显示

c# - 如何在列表框中列出 newtonsoft Jtoken 或 JObjects 子项

c# - 如何缓存数据库表以防止在 Asp.net C# mvc 中进行许多数据库查询

jquery - ASP.net MVC 3 jQuery 验证;禁用不显眼的 OnKeyUp?