Skip to content

Instantly share code, notes, and snippets.

@AThraen
Last active August 23, 2020 06:10
Show Gist options
  • Select an option

  • Save AThraen/7917f6e2af9aec098ef60bac24868887 to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/7917f6e2af9aec098ef60bac24868887 to your computer and use it in GitHub Desktop.
Episerver Forms Honeypot
using EPiServer.Forms.Core.PostSubmissionActor;
using EPiServer.Forms.Core.PostSubmissionActor.Internal;
using EPiServer.Forms.Helpers.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Demo.Web.Business.Forms
{
public class HoneypotActor : PostSubmissionActorBase, ISyncOrderedSubmissionActor
{
public int Order => 1;
public override object Run(object input)
{
var formContainerBlock = this.FormIdentity.GetFormBlock();
var allFormsElements = formContainerBlock.Form.Steps.SelectMany(st => st.Elements).Where(elem => elem.SourceContent is HoneyPotElementBlock).ToList();
if (allFormsElements.Any())
{
foreach (var fe in allFormsElements)
{
if (!string.IsNullOrEmpty(this.SubmissionData.Data[fe.ElementName]?.ToString()))
{
var result = new SubmissionActorResult();
result.CancelSubmit = true;
result.ErrorMessage = "Spam bot detected";
return result;
}
else
{
//Remove data for HoneyPotElement from final data.
this.SubmissionData.Data.Remove(fe.ElementName);
}
}
}
return "";
}
}
}
using EPiServer.DataAnnotations;
using EPiServer.Forms.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Demo.Web.Business.Forms
{
[ContentType(DisplayName = "HoneyPot Spam Avoidance", GUID = "B8248208-75D5-451A-8F5C-82898697B010", Description = "")]
public class HoneyPotElementBlock : ElementBlockBase
{
}
}
@model Demo.Web.Business.Forms.HoneyPotElementBlock
<style>
.ohnohoney {
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 0;
width: 0;
z-index: -1;
tabindex: -1;
}
</style>
<label class="ohnohoney" for="@Model.FormElement.Guid"></label>
<input type="text" name="@Model.FormElement.ElementName" autocomplete="off" placeholder="Your name" id="@Model.FormElement.Guid" class="ohnohoney" data-f-type="custom" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment