Tagged: __RequestVerificationToken

Optimizely Forms Returning 301 on Submission? The Real Cause Was an AntiForgery Token Conflict

Sometimes a form submission issue looks like a routing problem, a redirect issue, or even a load-balancer misconfiguration. In our case, the browser showed a 301 response during Optimizely Forms submission, but the actual root cause was much deeper.
 
Recently, while working on an Optimizely CMS 12 implementation, we encountered a strange issue where Optimizely Forms stopped submitting successfully after introducing AntiForgery token support for custom forms. At first glance, it appeared to be a standard redirect problem. However, after investigating the application logs and working with Optimizely Support, the underlying issue turned out to be an AntiForgery token validation conflict.
 
 

The Problem

As part of a security enhancement initiative, we introduced AntiForgery token support for custom forms within the solution.

The implementation included: AntiForgeryController.cs to generate and manage AntiForgery tokens for custom APIs and forms.


[Route("api/antiforgery")]

public class AntiForgeryController : ControllerBase

{

private readonly IAntiforgery _antiforgery;
private readonly string _antiForgeryToken;


public AntiForgeryController(IAntiforgery antiforgery, IConfiguration configuration)

{

_antiforgery = antiforgery;
_antiForgeryToken = configuration["AntiForgeryToken"];

}


[HttpGet("token")]

public IActionResult GetToken()

{

var tokens = _antiforgery.GetAndStoreTokens(HttpContext);


Response.Cookies.Append(

_antiForgeryToken,

tokens.RequestToken!,

new CookieOptions

{

HttpOnly = false,

Secure = true,

SameSite = SameSiteMode.Strict

});


return Ok(new

{

token = tokens.RequestToken

});

}
}

Shortly after deployment, users reported that Optimizely Forms were no longer submitting successfully.

The behaviour was unusual:

  • Form submissions failed intermittently.
  • Browser Developer Tools showed a 301 response during form submission.
  • Optimizely Forms that previously worked without issues could no longer be submitted.
  • Removing the AntiForgery implementation immediately restored functionality.

This clearly indicated that the issue was related to the newly introduced security implementation.

 

Initial Investigation

Our first assumption was that the problem was related to:

  • URL rewriting
  • Redirect rules
  • Front Door configuration
  • Reverse proxy settings
  • Form endpoint routing

However, none of these areas revealed any issues.

The turning point came when reviewing the application exception logs.

The logs contained the following error:

One or more errors occurred.
 
The required antiforgery request token
was not provided in either form field
“__RequestVerificationToken”
 
or header value
 
“AntiForgeryToken”
 

Further examination showed the exception being raised from Optimizely Forms security validation.
 
System.AggregateException
 
EPiServer.Forms.Internal.Security
.DefaultAntiForgeryValidator.Validate()
 
Microsoft.AspNetCore.Antiforgery
.AntiforgeryValidationException

At this point, it became clear that the 301 response was merely a symptom. The actual failure was occurring during AntiForgery validation.

 

Understanding the Root Cause

Optimizely Forms already includes built-in AntiForgery protection.

When we introduced a custom AntiForgery implementation, we unintentionally created a conflict between:

Custom AntiForgery Token
and
 
Optimizely Forms AntiForgery Validation
 

The Optimizely Forms submission pipeline expected to receive a valid request verification token using its own validation mechanism.

Instead, the request validation process encountered a mismatch and failed with:

AntiforgeryValidationException
 
because the required token was not available in one of the expected locations:
 
__RequestVerificationToken
 
or
 
AntiForgeryToken header
 
As a result, the form submission was rejected before reaching the normal processing pipeline.

The Resolution

The fix was surprisingly simple.

We aligned the AntiForgery configuration so that Optimizely Forms could correctly locate and validate the expected request token.

Configuration update: appsettings.json

{
“AntiForgeryToken”: “RequestVerificationToken”
}

After correcting the AntiForgery configuration and validating the token flow:

  • Optimizely Forms submissions started working again.
  • No further 301 submission issues occurred.
  • Standard Optimizely Forms security validation executed successfully.
  • Custom forms continued to function as expected.

Happy Optimizing!

 
0