From 6d6a54336ce63ba84fe9b5f74d33a40ffc127a0f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 29 Sep 2023 15:47:29 +0100 Subject: [PATCH] feat: add recaptcha validation to contact form --- OliverBooth/Controllers/ContactController.cs | 7 +++++++ OliverBooth/OliverBooth.csproj | 1 + OliverBooth/Pages/Contact/Index.cshtml | 2 ++ OliverBooth/Program.cs | 2 ++ 4 files changed, 12 insertions(+) diff --git a/OliverBooth/Controllers/ContactController.cs b/OliverBooth/Controllers/ContactController.cs index 2aa73d7..5dc16e2 100644 --- a/OliverBooth/Controllers/ContactController.cs +++ b/OliverBooth/Controllers/ContactController.cs @@ -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"]; diff --git a/OliverBooth/OliverBooth.csproj b/OliverBooth/OliverBooth.csproj index 6e7d24e..4802bc0 100644 --- a/OliverBooth/OliverBooth.csproj +++ b/OliverBooth/OliverBooth.csproj @@ -9,6 +9,7 @@ + diff --git a/OliverBooth/Pages/Contact/Index.cshtml b/OliverBooth/Pages/Contact/Index.cshtml index 02706e1..14eefe9 100644 --- a/OliverBooth/Pages/Contact/Index.cshtml +++ b/OliverBooth/Pages/Contact/Index.cshtml @@ -33,5 +33,7 @@ + + \ No newline at end of file diff --git a/OliverBooth/Program.cs b/OliverBooth/Program.cs index 76aab4a..beb0140 100644 --- a/OliverBooth/Program.cs +++ b/OliverBooth/Program.cs @@ -1,3 +1,4 @@ +using AspNetCore.ReCaptcha; using Markdig; using OliverBooth.Data.Blog; using OliverBooth.Data.Web; @@ -38,6 +39,7 @@ builder.Services.AddSingleton(); 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()) {