My ContactFormSurfaceController works very well in latest Umbraco, and ContactFormSurfaceController.cs look like this (also a model named ContactFormViewModel.cs, a Partial View file named ContactForm, and a Partial View Macro File named ContactForm exists):
using Neoweb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace Neoweb.Controllers
{
public class ContactFormSurfaceController : SurfaceController
{
// GET: ContactFormSurface
public ActionResult Index()
{
return PartialView("ContactForm", new ContactFormViewModel());
}
[HttpPost]
public ActionResult ContactFormSubmit(ContactFormViewModel model)
{
if (!ModelState.IsValid)
return CurrentUmbracoPage();
// Send email
MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New message";
message.From = new System.Net.Mail.MailAddress(model.Email, model.Name);
message.Body = model.Message;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
}
}
But now I want to implement reCAPTCHA 2.0 under the contact form like this (inside the same Fieldset):
I tried to implement reCaptcha codes like this direct into SurfaceController, but the reCapthca didn't stop the user - it send the ContactForm anyway, if all field are filled (Name, Email, Message):
using Neoweb.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace Neoweb.Controllers
{
public class ContactFormSurfaceController : SurfaceController
{
// GET: ContactFormSurface
public ActionResult Index()
{
return PartialView("ContactForm", new ContactFormViewModel());
}
[HttpPost]
public ActionResult ContactFormSubmit(ContactFormViewModel model)
{
//Validate Google recaptcha here
var response = System.Web.HttpContext.Current.Request["g-recaptcha-response"];
string secretKey = "xxxxxxxxxxxxxx HIDED xxxxxxxxxxxxxx";
var client = new WebClient();
var result = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
var obj = JObject.Parse(result);
var status = (bool)obj.SelectToken("success");
if (!ModelState.IsValid || !status)
return CurrentUmbracoPage();
// Send email
MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New message";
message.From = new System.Net.Mail.MailAddress(model.Email, model.Name);
message.Body = model.Message;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
}
}
Yes, (bool)obj.SelectToken("success") is set to true, so it must be something else.
Strange that if (!ModelState.IsValid || !status) return CurrentUmbracoPage(); fieldvalidation works fine, but not stop if reCaptcha gives an error (when 'status' is set to false):
ContactFormViewModel.cs (Model):
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Neoweb.Models
{
public class ContactFormViewModel
{
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
}
Both solutions should work, but it didn't. But after I transferred the entire solution over to the webhotel again, it now works also with reCAPTCHA (I didn't test mail server locally on my development machine, therefore probably these solutions have not been identical).
Now the entire solution is working, and it looks like this:
ContactFormSurfaceController.cs
using Neoweb.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace Neoweb.Controllers
{
public class ContactFormSurfaceController : SurfaceController
{
// GET: ContactFormSurface
public ActionResult Index()
{
if (TempData["reCaptcha"] == null)
{
ViewBag.MessageToUser = "Kontakt Neoweb";
}
return PartialView("ContactForm", new ContactFormViewModel());
}
[HttpPost]
public ActionResult ContactFormSubmit(ContactFormViewModel model)
{
//Validate Google recaptcha here
var response = Request["g-recaptcha-response"];
string secretKey = "xxxxxxxxxxxxxx HIDED xxxxxxxxxxxxxx";
var client = new WebClient();
var result = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secretKey, response));
var obj = JObject.Parse(result);
var status = (bool)obj.SelectToken("success");
TempData["reCaptcha"] = status;
if (!ModelState.IsValid)
{
ViewBag.MessageToUser = "Alle felt må utfylles";
return CurrentUmbracoPage();
}
if (!status)
{
// ViewBag.MessageToUser = status ? "reCaptcha validation success" : "reCaptcha validation failed";
ViewBag.MessageToUser = "Kryss av for ikke robot";
return CurrentUmbracoPage();
}
else
{
// Send email
MailMessage message = new MailMessage();
message.To.Add("[email protected]");
message.Subject = "New message";
message.From = new System.Net.Mail.MailAddress(model.Email, model.Name);
message.Body = model.Message;
SmtpClient smtp = new SmtpClient();
smtp.Send(message);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
}
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Neoweb.Models
{
public class ContactFormViewModel
{
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string Message { get; set; }
}
}
reCaptcha 2.0 and ContactFormSurfaceController
My ContactFormSurfaceController works very well in latest Umbraco, and ContactFormSurfaceController.cs look like this (also a model named ContactFormViewModel.cs, a Partial View file named ContactForm, and a Partial View Macro File named ContactForm exists):
But now I want to implement reCAPTCHA 2.0 under the contact form like this (inside the same Fieldset):
I tried to implement reCaptcha codes like this direct into SurfaceController, but the reCapthca didn't stop the user - it send the ContactForm anyway, if all field are filled (Name, Email, Message):
Any ideas?
Hi Tom,
Did you try to debug and look what answer comes from Google API?
Is this (bool)obj.SelectToken("success") really true?
We worked on reCAPTCHA 2.0 for umbraco Forms - https://our.umbraco.org/projects/collaboration/recaptcha-field-for-umbraco-forms/
You can look at source code
Thanks,
Alex
Yes,
(bool)obj.SelectToken("success")
is set to true, so it must be something else.Strange that
if (!ModelState.IsValid || !status) return CurrentUmbracoPage();
fieldvalidation works fine, but not stop if reCaptcha gives an error (when 'status' is set to false):ContactFormViewModel.cs (Model):
ContactForm.cshtml (Partial View)
ContactForm.cshtml (Partial View Macro File):
Hi Tom,
Its strange solution but maybe it will help you:
Thanks,
Alex
Both solutions should work, but it didn't. But after I transferred the entire solution over to the webhotel again, it now works also with reCAPTCHA (I didn't test mail server locally on my development machine, therefore probably these solutions have not been identical).
Now the entire solution is working, and it looks like this:
ContactFormSurfaceController.cs
ContactForm.cshtml
ContactFormViewModel.cs (Model):
ContactForm.cshtml (Partial View Macro File):
Everything is now OK, but ideas if the solution can be improved further will be appreciated
is working on a reply...