Skip to content

Instantly share code, notes, and snippets.

View scribanForm.js
var formData = $('input[name="text"]').val();
var token = $('input[name="__RequestVerificationToken"]').val();
var headersData = {};
headersData['__RequestVerificationToken'] = token;
$.ajax({
url: url,
method: 'POST',
data: formData,
dataType: 'json',
contentType: 'application/json',
View Scribanform.html
<form action="api/Confirm">
<input type="text" name="text" />
<input name="__RequestVerificationToken" type="hidden" value="{{sc_getantiforgerytoken}}">
<input type="submit" name="button" value="Confirm" />
</form>
View APIExample.cs
[HttpPost, ValidateFormAntiForgeryToken]
public ActionResult Confirm(TextModel text)
{
....
}
View ValidateFormAntiForgeryTokenAttribute.cs
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)
{
View Sitecore.Foundation.ScribanExtension.config
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore>
<pipelines>
<generateScribanContext>
<processor type="Sitecore.Foundation.ScribanExtensions.Scriban.GetAntiforgeryToken, Sitecore.Foundation.ScribanExtensions" resolve="true" />
</generateScribanContext>
</pipelines>
</sitecore>
</configuration>
View GetAntiforgeryToken.cs
using System;
using System.Web;
using System.Web.Helpers;
using Sitecore.XA.Foundation.Abstractions;
using Sitecore.XA.Foundation.Scriban.Pipelines.GenerateScribanContext;
using Scriban.Runtime;
namespace Sitecore.Foundation.ScribanExtensions.Scriban
{
public class GetAntiforgeryToken : IGenerateScribanContextProcessor
View FormExample.html
<form action="api/Confirm">
<input type="text" name="text" />
<input type="submit" name="button" value="Confirm" />
</form>
View Feature.Serialization.DI.config
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<services>
<configurator type="Feature.Serialization.DI.RegisterContainer, Feature.Serialization" />
</services>
</sitecore>
</configuration>
View RegisterContainer.cs
namespace Feature.Serialization.DI
{
using Feature.Serialization.Services;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
public class RegisterContainer : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
View IItemSerializationService.cs
namespace Feature.Serialization.Services
{
using Sitecore.Data.Items;
using Sitecore.Data.Serialization;
public interface IItemSerializationService
{
void DumpItem(Item item);
void DumpTree(Item item);
Item LoadItem(Item item, LoadOptions loadOptions);