[英]Captcha nuget package always Valid on ASP.NET MVC 5
我已經安裝了驗證碼插件並按照安裝說明進行了操作。 驗證碼在頁面上完美呈現,但在提交表單時,無論我輸入什么,ModelState.IsValid 始終為真。 顯然,如果驗證碼無法驗證,那么對我來說沒什么好處。
這是我的控制器:
[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();
}
這是我的觀點:
<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>
有誰知道為什么這對我不起作用? 謝謝你的幫助!
我不確定是否是同樣的問題。 我在模態彈出窗口中有一個 botdetect 驗證碼。 它工作正常,直到通過第一次驗證,然后它始終為真,所以我添加了一個 samplecaptcha.Reset() 並且 Model.IsValid 當它無效時再次為假。
@{
Layout = null;
MvcCaptcha sampleCaptcha = new MvcCaptcha("SampleCaptcha");
sampleCaptcha.Reset();
}
[等等]
//我和你有同樣的問題,但我用這種方式解決了
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");
}
}
在 RegistrationModel 添加屬性: CaptchaID ,CaptchaCode 用戶輸入,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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.