asp.net-mvc – 我如何编写一个ActionFilter来确保AntiForgeryTokens用于每个Pos
|
我想在每个HttpPost Action上使用一个ActionFilter来使用AntiForgeryTokens,该ActionFilter位于每个其他控制器继承的名为ControllerBase的控制器中. 我想通过创建一个继承自ValidateAntiForgeryToken的ActionFilter来做到这一点,该ActionFilter接受一个参数,告诉它要将自身应用的HTTP动词.然后,我想在ControllerBase上应用该过滤器,以确保在整个站点上检查AntiForgeryToken的每个POST操作. 我正在研究使用this solution,但是 > AuthorizationContext构造函数(ControllerContext)是一个过时的构造函数,我不知道如何使用推荐的AuthorizationContext(ControllerContext controllerContext,ActionDescriptor actionDescriptor)重建代码. 我应该如何重写ActionFilter以满足当前的非过时标准,并在每个[HttpPost]动词上正确使用防伪标记? 我是否必须在每种形式中都包含一个防伪标记(我在想)? (而不是自动生成 – 不要笑,我很好奇)更新:正如评论中指出的那样;是的,这必须在每个表格中完成. 以下是我的ControllerBase中的代码供参考: [UseAntiForgeryTokenOnPostByDefault]
public class ControllerBase : Controller
{
[AttributeUsage(AttributeTargets.Method,AllowMultiple = false)]
public class BypassAntiForgeryTokenAttribute : ActionFilterAttribute
{
}
[AttributeUsage(AttributeTargets.Class,AllowMultiple = false)]
public class UseAntiForgeryTokenOnPostByDefault : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (ShouldValidateAntiForgeryTokenManually(filterContext))
{
var authorizationContext = new AuthorizationContext(filterContext.Controller.ControllerContext);
//Use the authorization of the anti forgery token,//which can't be inhereted from because it is sealed
new ValidateAntiForgeryTokenAttribute().OnAuthorization(authorizationContext);
}
base.OnActionExecuting(filterContext);
}
/// <summary>
/// We should validate the anti forgery token manually if the following criteria are met:
/// 1. The http method must be POST
/// 2. There is not an existing [ValidateAntiForgeryToken] attribute on the action
/// 3. There is no [BypassAntiForgeryToken] attribute on the action
/// </summary>
private static bool ShouldValidateAntiForgeryTokenManually(ActionExecutingContext filterContext)
{
var httpMethod = filterContext.HttpContext.Request.HttpMethod;
//1. The http method must be POST
if (httpMethod != "POST") return false;
// 2. There is not an existing anti forgery token attribute on the action
var antiForgeryAttributes =
filterContext.ActionDescriptor.GetCustomAttributes(typeof (ValidateAntiForgeryTokenAttribute),false);
if (antiForgeryAttributes.Length > 0) return false;
// 3. There is no [BypassAntiForgeryToken] attribute on the action
var ignoreAntiForgeryAttributes =
filterContext.ActionDescriptor.GetCustomAttributes(typeof (BypassAntiForgeryTokenAttribute),false);
if (ignoreAntiForgeryAttributes.Length > 0) return false;
return true;
}
}
}
解决方法您不需要实例化任何AuthorizationContext或调用OnAuthorization方法,只需:if (ShouldValidateAntiForgeryTokenManually(filterContext))
{
AntiForgery.Validate(filterContext.HttpContext,null);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 使用多值键创建RouteValueDictionary
- 在Kendo-UI图表中刷新方法和重绘方法有什么不同?
- 免费ASP.Net和/或CSS主题
- asp.net-mvc如何更改宽度Html.TextBox
- asp.net – .NET 4中的SignalR支持
- asp.net-mvc-3 – 检查.NET MVC中的AngularJS $资源请求服务
- asp.net – ‘System.Web.UI.WebControls.TextBoxMode’不包
- .net-4.0 – Asp.Net 4.0 CacheItemPolicy滑动到期不正确?
- asp.net – 在代码而不是数据库中存储salt
- asp.net-mvc – 断言操作重定向到正确的操作/路由?
- 如何在非ASP.net上下文中使用C#中的数据验证属性
- asp.net-mvc – ASP.NET MVC DropDownListFor不支
- ASP.NET:Viewstate并以编程方式添加用户控件
- 从asp.net代码后面读表单认证cookie
- ASP脚本中的Python 500服务器错误
- asp.net – Page_Load中的Response.Redirect
- asp.net-mvc – 如何创建安装文件来安装MVC .net
- asp.net-mvc – 如何将SQL CE 4 CTP部署到共享主
- asp.net-mvc – ASP.NET MVC Web API缓存控制头部
- asp.net – 使用Ajax,在服务器或客户端生成额外的
