This commit is contained in:
2018-07-23 19:33:27 +00:00
parent a54087b5a5
commit bdbf4b6316
6 changed files with 92 additions and 19 deletions

View File

@@ -7,6 +7,11 @@ using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mail;
using System.Net;
//for captcha:
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
namespace contact.Pages
{
@@ -17,7 +22,7 @@ namespace contact.Pages
public string Name { get; set; }
[Required]
public string Company { get; set; }
[Required]
[Required]
public string Referrer { get; set; }
[Required, EmailAddress]
public string Email { get; set; }
@@ -30,7 +35,11 @@ namespace contact.Pages
public string Message { get; set; }
[BindProperty]
public RequestFormModel Contact { get; set; }
private readonly IConfiguration _configuration;
public RequestModel(IConfiguration configuration)
{
_configuration = configuration;
}
public ActionResult OnPost()
{
@@ -38,12 +47,16 @@ namespace contact.Pages
{
return Page();
}
//fuck those Russian spammers
if(Contact.Company.ToLowerInvariant()!="google")
SendMail();
if (!ReCaptchaPassed(
Request.Form["g-recaptcha-response"], // that's how you get it from the Request object
_configuration.GetSection("GoogleReCaptcha:secret").Value
))
{
//Return a fail code that will hopefully take us off the spammers list
return StatusCode(500);
}
//TODO: go to a successful submit page on the ayanova site
//return Redirect("/confirm.html");//sample quickie page I made up
SendMail();
return Redirect("https://ayanova.com/confirmed.htm");
}
@@ -100,5 +113,27 @@ namespace contact.Pages
{
Message = "Your contact page.";
}
public static bool ReCaptchaPassed(string gRecaptchaResponse, string secret)
{
HttpClient httpClient = new HttpClient();
var res = httpClient.GetAsync($"https://www.google.com/recaptcha/api/siteverify?secret={secret}&response={gRecaptchaResponse}").Result;
if (res.StatusCode != HttpStatusCode.OK)
{
// logger.LogError("Error while sending request to ReCaptcha");
return false;
}
string JSONres = res.Content.ReadAsStringAsync().Result;
dynamic JSONdata = JObject.Parse(JSONres);
if (JSONdata.success != "true")
{
return false;
}
return true;
}
}
}