Last active
August 29, 2015 13:56
-
-
Save jamescrowley/9154704 to your computer and use it in GitHub Desktop.
Global CSRF checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SkipCSRFCheck : Attribute | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment