- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用AntiForgeryTokens
在每个 HttpPost 操作上使用名为 ControllerBase
的 Controller 中的 ActionFilter每个其他 Controller 都继承自。
我想通过创建一个继承自 ValidateAntiForgeryToken
的 ActionFilter 来做到这一点。它接受一个参数,告诉它要应用哪些 HTTP 动词。然后我想在 ControllerBase
上应用该过滤器确保AntiForgeryToken
检查整个站点上的每个 POST 操作。
我正在考虑使用 this solution , 但
AuthorizationContext Constructor (ControllerContext)
是一个过时的构造函数,我不确定如何使用推荐的 AuthorizationContext(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
重建代码。 . A required anti-forgery token was not supplied or was invalid
在每次发布操作之后。 [HttpPost]
上正确使用防伪 token 动词?
[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;
}
}
}
最佳答案
我使用了以下方法:
public class SkipCSRFCheckAttribute : Attribute
{
}
public class AntiForgeryTokenFilter : IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (IsHttpPostRequest(filterContext) && !SkipCsrfCheck(filterContext))
AntiForgery.Validate();
}
private static bool IsHttpPostRequest(AuthorizationContext filterContext)
{
return filterContext.RequestContext.HttpContext.Request.HttpMethod == HttpMethod.Post.ToString();
}
private static bool SkipCsrfCheck(AuthorizationContext filterContext)
{
return filterContext.ActionDescriptor.GetCustomAttributes(typeof (SkipCSRFCheck), false).Any();
}
}
关于asp.net-mvc - 我将如何编写一个 Action 过滤器来确保每个 Post 操作都使用防伪 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781191/
当我尝试在 ASP.net Web 应用程序上运行性能测试时,我总是收到“防伪 cookie token 和表单字段 token 不匹配”。或者“无法解密防伪 token 。如果此应用程序由网络场或集
我有一个发布到 Azure 的 MVC Web 应用程序。由于在编辑 View 中包含@html.antiforgerytoken(),每当我单击编辑操作链接时,我都会收到错误。所以我删除了 View
我有 2 个 Web 应用程序项目,都位于 TFS 源代码管理中。第一个项目不会导致 AntiForgery token 出现问题。 这是错误 System.Web.WebPages.dll 中发生“
我有一个应该是 iFramed 的 MVC View 。它的多个实例可能在同一个主机页面中被 iFramed。在我看来,我有这个: @Html.AntiForgeryToken() 我用它来尝试确保对
我们使用 ASP.NET MVC 的默认防伪技术。最近一家安全公司对表格进行了扫描,并注意到他们可以多次使用相同的 _RequestVerificationToken 组合(cookie + 隐藏字段
我在使用防伪 token 时遇到问题 :(我已经创建了自己的 User 类,它运行良好,但现在每当我转到 /Account/Register 页面时都会收到错误消息。错误是: A claim of t
我正在使用 ASP.NET Identity 登录的应用程序中开发注销功能。我可以成功登录,但是当我注销然后尝试再次登录时,我收到以下消息: The provided anti-forgery tok
定义 ValidateAntiForgeryToken 属性时。盐的复杂度应该是多少? 例如,超过 X 个字符的字母数字字符是否合适?是否也应该有符号。 [ValidateAntiForgeryTok
我有一个问题,我最近在我的所有表单中都放置了防伪 token ,并在我的 Controller 中放置了 ValidateAntiForgeryToken 属性。 但我的用户经常填写表格,然后返回浏览
在没有 ssl 的情况下向服务器发出请求时,我实际上可以以纯文本形式看到 MVC3 框架生成的验证 token key 。 此 key 存储在名为:_RequestVerificationToken_
我们在 ASP.NET MVC 4 站点上使用 Amazon EC2 Elastic Beanstalk,并且在用户尝试登录后收到错误消息: The anti-forgery token could
我有一个表格: @using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) { @Html.AntiForgeryToken() @H
在 nim 中,jester 模块提供 Web API 服务,我对此表示感谢,但是我们是否支持跨站点防伪造 token ? (针对CSRF攻击) 就像 ASP.net 提供的那样。 最佳答案 没有。有
在 ASP.NET MVC 1.0 中,有一个新功能用于处理跨站请求伪造安全问题: [ValidateAntiForgeryToken] public ViewResult SubmitUpdat
我正在运行 IIS7、.NET 4.5 并且有一个 MVC2 站点。 我收到错误消息“此操作需要 IIS 集成管道模式。”当我导航到一个页面(一个 aspx View )时,上面有一个带有 AntiF
我收到以下错误: {"A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or
我的 Web Api 站点有一个 AccountController,它使用登录的默认实现: // POST: /Account/Login [HttpPost] [AllowAnonymous] [
我正在尝试在 VS2012 中为 MVC 站点进行 Web 测试。 其中一种情况是登录并浏览产品列表,选择您想要的产品并进入购买页面。 问题是当 web 测试运行时,我收到一个关于防伪 token 的
您好,我可以像这样使用 vue js axios 将帖子发送到 Asp .NET Core 2.2 中的 Controller axios({
我正在使用 ELMAH 来处理我的 MVC 站点中的错误,并且在过去的几周里我注意到我抛出了一些 CryptographicExceptions。消息是: System.Security.Crypto
我是一名优秀的程序员,十分优秀!