feat: add recaptcha validation to contact form

This commit is contained in:
Oliver Booth 2023-09-29 15:47:29 +01:00
parent 6d572c7b6b
commit 6d6a54336c
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
4 changed files with 12 additions and 0 deletions

View File

@ -39,6 +39,13 @@ public class ContactController : Controller
return RedirectToPage("/Contact/Index");
}
if (!ModelState.IsValid)
{
_logger.LogError("Captcha validation failed");
TempData["Success"] = false;
return RedirectToPage("/Contact/Result");
}
IFormCollection form = Request.Form;
StringValues name = form["name"];
StringValues email = form["email"];

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
<PackageReference Include="AspNetCore.ReCaptcha" Version="1.7.0" />
<PackageReference Include="BCrypt.Net-Core" Version="1.6.0"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/>
<PackageReference Include="MailKit" Version="4.1.0"/>

View File

@ -33,5 +33,7 @@
<textarea class="form-control" id="message" name="message" rows="5" required placeholder="What's on your mind?"></textarea>
</div>
<recaptcha/>
<button class="btn btn-primary" style="margin-top: 10px;">Submit</button>
</form>

View File

@ -1,3 +1,4 @@
using AspNetCore.ReCaptcha;
using Markdig;
using OliverBooth.Data.Blog;
using OliverBooth.Data.Web;
@ -38,6 +39,7 @@ builder.Services.AddSingleton<IProjectService, ProjectService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews();
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddReCaptcha(builder.Configuration.GetSection("ReCaptcha"));
if (builder.Environment.IsProduction())
{