[英]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.