简体   繁体   English

Captcha nuget 包在 ASP.NET MVC 5 上始终有效

[英]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.验证码在页面上完美呈现,但在提交表单时,无论我输入什么,ModelState.IsValid 始终为真。 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!谢谢你的帮助!

I'm not sure if it's the same problem.我不确定是否是同样的问题。 I've got a botdetect captcha in a modal popup.我在模态弹出窗口中有一个 botdetect 验证码。 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.它工作正常,直到通过第一次验证,然后它始终为真,所以我添加了一个 samplecaptcha.Reset() 并且 Model.IsValid 当它无效时再次为假。

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

[etc] [等等]

//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在 RegistrationModel 添加属性: CaptchaID ,CaptchaCode 用户输入,CurrentInstanceID

cshtml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NuGet package 用于 ASP.NET Core 5 MVC - NuGet package for ASP.NET Core 5 MVC 测试ModelState在asp.net mvc中始终有效 - Testing ModelState is always valid in asp.net mvc NuGet 包 (.NET Standard 2.x) 破坏 ASP.NET MVC5 应用程序(.NET 完整框架) - NuGet package (.NET Standard 2.x) breaking ASP.NET MVC5 application (.NET full framework) Asp.Net 5 Nuget软件包错误 - Asp.Net 5 Nuget package error ASP.NET NuGet Package 依赖关系不会更新 - ASP.NET NuGet Package Dependency Will Not Update 如何在ASP.NET MVC中使用Coinpayments.net nuget包? - How to use Coinpayments.net nuget package with ASP.NET MVC? 在ASP.NET MVC 6中更新NuGet包 - Updating NuGet packages in ASP.NET MVC 6 在Docker中创建新的ASP.NET Core MVC应用程序时,NuGet软件包的还原非常缓慢 - NuGet package restore extremely slow when creating new ASP.NET Core MVC app in Docker Telerik Rad Captcha-验证始终返回false-ASP.NET - Telerik Rad Captcha - Validation always returns false - ASP.NET Visual Studio Professional 2012错误:此项目引用ASP.NET MVC项目中缺少的NuGet包 - Visual Studio Professional 2012 error: This project references NuGet package(s) that are missing in ASP.NET MVC project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM