Scott Purtan
Scott Purtan

Reputation: 237

Issues with ReCaptcha in MVC

I have followed the answer to this question [question] How to implement reCaptcha V3 in ASP.NET

Everything is fine up to the controller. I am getting an error that it "doesnt exist in the current context. Am i missing something in my Controller Action?

ViewModel -

public class LoginViewModel
{
    [Required]
    [Display(Name = "User Name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

    public static bool ReCaptchaPassed(string gRecaptchaResponse)
    {
        HttpClient httpClient = new HttpClient();
        var res = httpClient.GetAsync($"https://www.google.com/recaptcha/api/siteverify?secret=My-Secret-Key no quotes&response={gRecaptchaResponse}").Result;
        if (res.StatusCode != HttpStatusCode.OK)
            return false;

        string JSONres = res.Content.ReadAsStringAsync().Result;
        dynamic JSONdata = JObject.Parse(JSONres);
        if (JSONdata.success != "true")
            return false;

        return true;
    }
}

login Post Async -

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        //if (!ModelState.IsValid)
        //{
        //    return View(model);
        //}
        if (!ModelState.IsValid) return View(model);
        else
        {
            if (!ReCaptchaPassed(Request.Form["BestenReCaptcha"]))
            {
                ModelState.AddModelError(string.Empty, "You failed the CAPTCHA.");
                return View();
            }
        }
          /// Other actions ///
     }

Error - The name 'ReCaptchaPassed' does not exist in the current context

I have to missing something, the answer was marked as a fix and no-one else posted that they had an issue with it. I would have asked it there, however the question is an older one. Thanks for your help!

Upvotes: 1

Views: 432

Answers (1)

Jasen
Jasen

Reputation: 14250

The context for the method call in the linked answer was the same class. Since you are calling the static method from a different class, you would call it as

LoginViewModel.RecaptchaPassed(Request.Form["BestenReCaptcha"])

Upvotes: 1

Related Questions