Last active
February 24, 2022 17:16
-
-
Save gerhart92/d9ae986777da9e96032610ba4a681881 to your computer and use it in GitHub Desktop.
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
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