Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Philip Christianto 20 posts 150 karma points
    Apr 17, 2017 @ 12:49
    Philip Christianto
    0

    google recapcha on surface controller

    i have a model and surface controller for contact form and i want to put a google recapcha on it. how do i do this?

    public class ContactModel
    {
        [Required]
        [DisplayName("First name")]
        public string Name { get; set; }
        [Required]
        [DisplayName("Email address")]
        public string Email { get; set; }
    }
    

    the form partial

    @Html.TextBoxFor(x => x.Name, new { @class= "nameInput" })
    @Html.TextBoxFor(x => x.LastName, new { @class= "lastNameInput" })
    <input type="submit" value="Send" class="buttons send" />
    

    and the controller

        public ActionResult Contact(ContactModel model)
        {
            if (ModelState.IsValid)
            {
            }
            return CurrentUmbracoPage();
        }
    
  • John ben 78 posts 293 karma points
    Apr 17, 2017 @ 15:21
    John ben
    100

    Hi Philip Christianto.

                [HttpPost]
                [ValidateAntiForgeryToken]
                public ActionResult Contact(ContactModel model)
                {
                    var response = Request["g-recaptcha-response"];
                    string secretKey = "6LfJeQgUAAAAAEhworh47HsU1-zSUH3LlWmLd3yx";  // Ur secretKey
                    var webClient = new WebClient();
                    var result = webClient.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 == true)
                    {
                        //Ur code to send message
                        TempData["ContactSuccess"] = "success";
                        return RedirectToCurrentUmbracoPage();
                    }
    
                    return CurrentUmbracoPage();
                }
    

    and in ur view :

    <div class="col-md-12">
                    <div class="g-recaptcha" data-sitekey="6LfJeQgUAAAAABd4W9M8iekzfifYTlX71Ktsa8bY"></div>
                </div>
    

    don't forget to add

    <script src="https://www.google.com/recaptcha/api.js" ></script>
    
  • Alex Skrypnyk 6054 posts 23213 karma points MVP 5x admin c-trib
    Jul 10, 2017 @ 09:00
    Alex Skrypnyk
    0

    Hi

    For Umbraco Forms forms you can use ReCaptcha package:

    https://our.umbraco.org/projects/collaboration/recaptcha-field-for-umbraco-forms/

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft