Commit: Contact Honeypot

This commit is contained in:
clive
2024-11-24 09:10:59 +11:00
parent 307a5b8548
commit d487531541
4 changed files with 61 additions and 33 deletions

View File

@@ -3,6 +3,7 @@ using System.Net;
using clivelancaster.Models; using clivelancaster.Models;
using MailKit.Net.Smtp; using MailKit.Net.Smtp;
using MimeKit; using MimeKit;
using System.Xml.Linq;
namespace clivelancaster.Controllers namespace clivelancaster.Controllers
{ {
@@ -31,32 +32,40 @@ namespace clivelancaster.Controllers
var bodyBuilder = new BodyBuilder 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(); 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!"; 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(); return View();

View File

@@ -4,21 +4,27 @@ namespace clivelancaster.Models
{ {
public class ContactFormModel public class ContactFormModel
{ {
[Required]
[Display(Name = "Your Name")] [Display(Name = "Your Name")]
public required string Name { get; set; } public string? Name { get; set; }
[Required] [Required]
[EmailAddress] [EmailAddress]
[Display(Name = "Your Email")] [Display(Name = "Your Email")]
public required string Email { get; set; } public required string Email { get; set; }
[Required]
[Display(Name = "Subject")] [Display(Name = "Subject")]
public required string Subject { get; set; } public string? Subject { get; set; }
[Required]
[Display(Name = "Message")] [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; }
} }
} }

View File

@@ -19,9 +19,10 @@
<form asp-action="Index" method="post"> <form asp-action="Index" method="post">
<div class="form-group"> <div class="form-group">
<label asp-for="Name"></label> <label asp-for="Name" class="incognito-field"></label>
<input asp-for="Name" class="form-control" /> <input asp-for="Name" class="form-control incognito-field" autocomplete="off" tabindex="-1" />
<span asp-validation-for="Name" class="text-danger"></span> <label asp-for="NameIncognito"></label>
<input asp-for="NameIncognito" class="form-control" />
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="Email"></label> <label asp-for="Email"></label>
@@ -29,14 +30,16 @@
<span asp-validation-for="Email" class="text-danger"></span> <span asp-validation-for="Email" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="Subject"></label> <label asp-for="Subject" class="incognito-field"></label>
<input asp-for="Subject" class="form-control" /> <input asp-for="Subject" class="form-control incognito-field" autocomplete="off" tabindex="-1" />
<span asp-validation-for="Subject" class="text-danger"></span> <label asp-for="SubjectIncognito"></label>
<input asp-for="SubjectIncognito" class="form-control" />
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="Message"></label> <label asp-for="Message" class="incognito-field"></label>
<textarea asp-for="Message" class="form-control"></textarea> <textarea asp-for="Message" class="form-control incognito-field" autocomplete="off" tabindex="-1"></textarea>
<span asp-validation-for="Message" class="text-danger"></span> <label asp-for="MessageIncognito"></label>
<textarea asp-for="MessageIncognito" class="form-control"></textarea>
</div> </div>
<div class="p-2"></div> <div class="p-2"></div>
<button type="submit" class="btn btn-primary">Send Message</button> <button type="submit" class="btn btn-primary">Send Message</button>

View File

@@ -20,6 +20,16 @@
border-color: #0473b3; 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) { @media (prefers-color-scheme: dark) {
: root { : root {
--current-background: var(--dark-background); --current-background: var(--dark-background);