public ContactResult Submit(ContactModel contact, ContactSettings contactSettings) { var result = new ContactResult(); if (contact == null) throw new ArgumentNullException("Missing contact form data"); if (contactSettings == null) throw new ArgumentNullException("Missing settings"); if (logger != null) logger.LogInformation("Contact: {0} / {1} / {2} [Email: {3}, Post: {4}, Recaptcha: {5}]", contact.ContactName, contact.Email, contact.Phone, contactSettings.EmailSettings?.Enabled, contactSettings.PostSettings?.Enabled, contactSettings.RecaptchaSettings?.Enabled); if (contactSettings.RecaptchaSettings != null && contactSettings.RecaptchaSettings.Enabled && !string.IsNullOrEmpty(contactSettings.RecaptchaSettings.RecaptchaKey)) { // Check Recaptcha var recaptchaService = new RecaptchaService(); result.RecaptchaResult = recaptchaService.Validate(contactSettings.RecaptchaSettings, logger); if (result.RecaptchaResult.ServiceResultType != ServiceResultType.Success) return result; // Stop processing immediately } if (contactSettings.EmailSettings != null && contactSettings.EmailSettings.Enabled) { var emailService = new EmailService(); result.EmailResult = emailService.SendEmail(contact, contactSettings.EmailSettings, logger); } if (contactSettings.PostSettings != null && contactSettings.PostSettings.Enabled && !string.IsNullOrEmpty(contactSettings.PostSettings.PostURL)) { var postService = new PostService(); result.PostResult = postService.Post(contact, contactSettings.PostSettings, logger); } return result; }
public HttpResponseMessage ValidateCaptcha(RecaptchaPostRequest model) { //check if the model is valid if (!ModelState.IsValid) { //add to error log ErrorLogService svc = new ErrorLogService(); ErrorLogAddRequest error = new ErrorLogAddRequest(); error.ErrorFunction = "Sabio.Web.Controllers.Api.ValidateCaptcha"; error.ErrorMessage = ModelState.ToString(); error.UserId = UserService.UserSelect().PersonId; svc.ErrorLogInsert(error); //if the model isn't valid, return a error response return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } //instantiate the item response model ItemResponse <bool> response = new ItemResponse <bool>(); //grab the values from the model string key = model.secret; string request = model.response; //send POST request via the service we made which returns a bool bool isCaptchaValid = RecaptchaService.Validate(request, key); //save the bool value in our response model response.Item = isCaptchaValid; //return the response return(Request.CreateResponse(HttpStatusCode.OK, response)); }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { if (!_webHostEnvironment.IsDevelopment()) { if (HttpMethods.IsPost(context.HttpContext.Request.Method)) { var recaptcha = await _recaptcha.Validate(context.HttpContext.Request); if (!recaptcha.success || recaptcha.score != 0 && recaptcha.score < _minimumScore) { context.ModelState.AddModelError(string.Empty, _errorMessage); } } } await next(); }
public HttpResponseMessage sendCaptcha(RecaptchaRequest model) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } ItemResponse <bool> response = new ItemResponse <bool>(); string key = model.secret; string request = model.response; bool isCaptchaValid = RecaptchaService.Validate(request, key); response.Item = isCaptchaValid; return(Request.CreateResponse(HttpStatusCode.OK, response)); }
public IActionResult OnPost() { // Call custom validator var validator = new ContactModelValidator(); var results = validator.Validate(ContactModel); results.AddToModelState(ModelState, "ContactModel."); // Check Recaptcha if (settings.RecaptchaEnabled) { var recaptchaService = new RecaptchaService(); var recaptchaResponse = recaptchaService.Validate( new RecaptchaSettings { Enabled = true, RecaptchaKey = settings.RecaptchaSecretKey, RecaptchaResponse = Request.Form["g-recaptcha-response"] }, logger); if (recaptchaResponse.ServiceResultType != ServiceResultType.Success) { ModelState.AddModelError("Recaptcha", recaptchaResponse.Message); } } if (!ModelState.IsValid) { return(Page()); } HttpContent content = new StringContent(JsonConvert.SerializeObject(ContactModel), Encoding.UTF8, "application/json"); var resultOk = post.Post(settings.APIEndpoint, content) && post.Post(settings.ContactFormWebEndpoint, content); if (resultOk) { return(new RedirectToPageResult("ContactCS", new { s = 1 })); } else { Message = "Error calling API."; return(Page()); } }
public IActionResult OnPost([FromBody] ContactModel ContactModel) { // Call custom validator var validator = new ContactModelValidator(); var results = validator.Validate(ContactModel); results.AddToModelState(ModelState, null); // Check Recaptcha if (settings.RecaptchaEnabled) { var recaptchaService = new RecaptchaService(); var recaptchaResponse = recaptchaService.Validate( new RecaptchaSettings { Enabled = true, RecaptchaKey = settings.RecaptchaSecretKey, RecaptchaResponse = ContactModel.RecaptchaResponse }, logger); if (recaptchaResponse.ServiceResultType != ServiceResultType.Success) { ModelState.AddModelError("Recaptcha", recaptchaResponse.Message); } } if (!ModelState.IsValid) { return(new JsonResult(ModelState.Values)); } HttpContent content = new StringContent(JsonConvert.SerializeObject(ContactModel), Encoding.UTF8, "application/json"); var resultOk = post.Post(settings.APIEndpoint, content) && post.Post(settings.ContactFormWebEndpoint, content); return(new JsonResult(new { Status = resultOk ? "OK" : "Error calling API." })); }