From d48753154189f60c8aa2abe15813ea30f0da3508 Mon Sep 17 00:00:00 2001 From: clive Date: Sun, 24 Nov 2024 09:10:59 +1100 Subject: [PATCH] Commit: Contact Honeypot --- Controllers/ContactController.cs | 45 +++++++++++++++++++------------- Models/ContactFormModel.cs | 18 ++++++++----- Views/Contact/Index.cshtml | 21 ++++++++------- wwwroot/css/site.css | 10 +++++++ 4 files changed, 61 insertions(+), 33 deletions(-) diff --git a/Controllers/ContactController.cs b/Controllers/ContactController.cs index bf15d9d..62da2f5 100644 --- a/Controllers/ContactController.cs +++ b/Controllers/ContactController.cs @@ -3,6 +3,7 @@ using System.Net; using clivelancaster.Models; using MailKit.Net.Smtp; using MimeKit; +using System.Xml.Linq; namespace clivelancaster.Controllers { @@ -31,32 +32,40 @@ namespace clivelancaster.Controllers var bodyBuilder = new BodyBuilder { - TextBody = $"Name: {model.Name}\nEmail: {model.Email}\n\nSubject: {model.Subject}\n\nMessage:\n{model.Message}" + TextBody = $"Name: {model.NameIncognito}\nEmail: {model.Email}\n\nSubject: {model.SubjectIncognito}\n\nMessage:\n{model.MessageIncognito}" }; message.Body = bodyBuilder.ToMessageBody(); - try + // Honeypot Check + if (model.Name != null | model.Subject != null | model.Message != null) { - using (var client = new SmtpClient()) - { - // Retrieve SMTP settings from appsettings.json - var smtpHost = _configuration["SmtpSettings:Host"]; - int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]); - var smtpUsername = _configuration["SmtpSettings:Username"]; - var smtpPassword = _configuration["SmtpSettings:Password"]; - - client.Connect(smtpHost, smtpPort, true); - client.Authenticate(smtpUsername, smtpPassword); - await client.SendAsync(message); - client.Disconnect(true); - } - ViewBag.Message = "Message sent successfully!"; } - catch + else { - ViewBag.Message = "Error sending message."; + try + { + using (var client = new SmtpClient()) + { + // Retrieve SMTP settings from appsettings.json + var smtpHost = _configuration["SmtpSettings:Host"]; + int smtpPort = int.Parse(s: _configuration["SmtpSettings:Port"]); + var smtpUsername = _configuration["SmtpSettings:Username"]; + var smtpPassword = _configuration["SmtpSettings:Password"]; + + client.Connect(smtpHost, smtpPort, true); + client.Authenticate(smtpUsername, smtpPassword); + await client.SendAsync(message); + client.Disconnect(true); + } + + ViewBag.Message = "Message sent successfully!"; + } + catch + { + ViewBag.Message = "Error sending message."; + } } return View(); diff --git a/Models/ContactFormModel.cs b/Models/ContactFormModel.cs index 08ce2d8..5b7e1ca 100644 --- a/Models/ContactFormModel.cs +++ b/Models/ContactFormModel.cs @@ -4,21 +4,27 @@ namespace clivelancaster.Models { public class ContactFormModel { - [Required] [Display(Name = "Your Name")] - public required string Name { get; set; } + public string? Name { get; set; } [Required] [EmailAddress] [Display(Name = "Your Email")] public required string Email { get; set; } - [Required] [Display(Name = "Subject")] - public required string Subject { get; set; } + public string? Subject { get; set; } - [Required] [Display(Name = "Message")] - public required string Message { get; set; } + public string? Message { get; set; } + + [Display(Name = "Your Name")] + public string? NameIncognito { get; set; } + + [Display(Name = "Subject")] + public string? SubjectIncognito { get; set; } + + [Display(Name = "Message")] + public string? MessageIncognito { get; set; } } } diff --git a/Views/Contact/Index.cshtml b/Views/Contact/Index.cshtml index f44c737..8e9ac70 100644 --- a/Views/Contact/Index.cshtml +++ b/Views/Contact/Index.cshtml @@ -19,9 +19,10 @@
- - - + + + +
@@ -29,14 +30,16 @@
- - - + + + +
- - - + + + +
diff --git a/wwwroot/css/site.css b/wwwroot/css/site.css index eda70b3..4e1fd5f 100644 --- a/wwwroot/css/site.css +++ b/wwwroot/css/site.css @@ -20,6 +20,16 @@ border-color: #0473b3; } +.incognito-field { + opacity: 0; + position: absolute; + top: 0; + left: 0; + height: 0; + width: 0; + z-index: -1; +} + @media (prefers-color-scheme: dark) { : root { --current-background: var(--dark-background);