Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Captcha nuget package always Valid on ASP.NET MVC 5

I've installed the captcha plugin and followed the setup instructions. The captcha renders perfectly on the page but when submit the form, the ModelState.IsValid is always true, no matter what I enter. Obviously, if captcha won't validate then it's not much good to me.

Here's my controller:

        [HttpPost]
    [CaptchaValidation("CaptchaCode", "SampleCaptcha", "Incorrect CAPTCHA code!")]
    public ActionResult Register(AccountModel model)
    {
        if (!ModelState.IsValid)
        {
            // TODO: Captcha validation failed, show error message
        }
        else
        {
            // TODO: Captcha validation passed, proceed with protected action
        }

        return View();
    }

And here's my view:

<link href="@BotDetect.Web.CaptchaUrls.Absolute.LayoutStyleSheetUrl" rel="stylesheet" type="text/css" />

<form class="form-horizointal" action="@Url.Action("Register", "Account")" method="POST">
    <div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-sm-2 control-label" })
        @Html.TextBoxFor(m => m.FirstName, new { placeholder = "First Name"})
        @Html.LabelFor(m => m.LastName, new { @class = "control-label", placeholder = "Last Name" })
        @Html.TextBoxFor(m => m.LastName, new { placeholder = "Last Name" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new {@class = "col-sm-2 control-label", placeholder = "Email"})
        @Html.TextBoxFor(m => m.Email, new {placeholder = "Email"})
    </div>
    <div class="form-group">
        @{ MvcCaptcha sampleCaptcha = new MvcCaptcha("SampleCaptcha"); }
        @Html.Captcha(sampleCaptcha)
        @Html.TextBox("CaptchaCode")
    </div>
    <div class="form-group">
        captcha goes here
    </div>
    <div class="form-group">
        <input type="submit" class="btn btn-default" value="Register"/>
    </div>
</form>

Does anyone have any idea why this isn't working for me? Thanks for your help!

like image 644
Dan Csharpster Avatar asked Mar 17 '23 05:03

Dan Csharpster


2 Answers

I'm not sure if it's the same problem. I've got a botdetect captcha in a modal popup. It' works ok until passes the first validation ok, after then it's always true so i added a samplecaptcha.Reset() and the Model.IsValid is false again when it's not valid.

@{
    Layout = null;
    MvcCaptcha sampleCaptcha = new MvcCaptcha("SampleCaptcha");
    sampleCaptcha.Reset();
}

[etc]

like image 160
MirlvsMaximvs Avatar answered Mar 26 '23 01:03

MirlvsMaximvs


//I had the same problem with you, but i solved it with this way

    public ActionResult RegistrationComp(NewRegistration data, bool captchaValid)
    {
        try
        {
            captchaValid = MvcCaptcha.Validate(data.CaptchaID, data.CaptchaCode, data.CurrentInstanceID);

            MvcCaptcha.ResetCaptcha(data.CaptchaID);

            if (!captchaValid)
                return this.Json("false");
            else
                return this.Json("true");

        }
        catch (Exception ex)
        {
            return this.Json("false");
        }
    }

in RegistrationModel add properties : CaptchaID ,CaptchaCode the user input,CurrentInstanceID

cshtml

MvcCaptcha exampleCaptcha = new MvcCaptcha("C");

exampleCaptcha.UserInputID = "CC";

<script>
 $.post("@Url.Action("RegistrationComp", "Authentication")", { CaptchaCode: $("#CC").val() , CaptchaID: '@exampleCaptcha.CaptchaId',CurrentInstanceID: '@exampleCaptcha.CurrentInstanceId' }, function (data) {});
</script>
like image 34
kchr Avatar answered Mar 26 '23 03:03

kchr