Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescrowley/9154704 to your computer and use it in GitHub Desktop.
Save jamescrowley/9154704 to your computer and use it in GitHub Desktop.
Global CSRF checking
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();
}
}
public class SkipCSRFCheck : Attribute
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment