Skip to content

Instantly share code, notes, and snippets.

@gerhart92
Last active February 24, 2022 17:16
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 gerhart92/d9ae986777da9e96032610ba4a681881 to your computer and use it in GitHub Desktop.
Save gerhart92/d9ae986777da9e96032610ba4a681881 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Helpers;
using System.Web.Mvc;
namespace Sitecore.Foundation.Forms.Validation
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ValidateFormAntiForgeryTokenAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
var httpContext = filterContext.HttpContext;
var cookie = httpContext.Request.Cookies[AntiForgeryConfig.CookieName];
string formToken = "";
if (httpContext.Request.Headers["__RequestVerificationToken"] != null)
{
var tokenHeaders = httpContext.Request.Headers["__RequestVerificationToken"];
string[] tokens = tokenHeaders.Split(':');
if (tokens.Length == 2)
{
formToken = tokens[1].Trim();
}
}
AntiForgery.Validate(cookie != null ? cookie.Value : null, formToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment