BotDetect 和 ASPNET Razor Pages 未验证

BotDetect and ASPNET Razor Pages is not validating

我决定在我的项目中使用 BotDetect Captcha 来阻止垃圾邮件,但是,由于 Razor Pages 不支持过滤器,我无法检查用户是否输入了正确的验证码。

在他们的网站上,他们说要使用此属性来检查验证码是否有效

[CaptchaValidationActionFilter("CaptchaCode", "ExampleCaptcha", "Wrong Captcha!")]

但是,razor 页面不允许页面方法上的属性。

深入研究属性的源代码,我发现了这个

MvcCaptcha mvcCaptcha = new MvcCaptcha(this.CaptchaId);
if (mvcCaptcha.IsSolved) { }

然而,当我直接在 OnPost 方法中尝试该代码时,mvcCaptch.IsSolved 总是 returns false。

检查会话变量还会显示此控件工作所需的所有 BDC_ 值,所以我在这里碰壁了。希望有人能帮助我。谢谢。

官方文档,如果有帮助的话,我在网站上找不到任何对 Razor Pages 的引用 https://captcha.com/mvc/mvc-captcha.html

我发现有一个 CaptchaModelStateValidation 属性可以应用于绑定到验证码输入的 Razor 页面模型 属性。这样您就可以在 ModelState.

中自动获得验证

这是一个验证验证码的示例模型。

public class CaptchaValidatorModel : PageModel
{
   public void OnPost()
   {
      if (ModelState.IsValid)
      {
         // Perform actions on valid captcha.
      }
   }

   [BindProperty]
   [Required] // You need this so it is not valid if the user does not input anything
   [CaptchaModelStateValidation("ExampleCaptcha")]
   public string CaptchaCode { get; set; }
}

该页面使用文档示例中提供的代码。

@page
@model CaptchaWebApplication.Pages.CaptchaValidatorModel
@{
   ViewData["Title"] = "Captcha";
}
<form method="post">
   <label asp-for="CaptchaCode">Retype the code from the picture:</label>
   <captcha id="ExampleCaptcha" user-input-id="CaptchaCode" />
   <div class="actions">
      <input asp-for="CaptchaCode" />
      <input type="submit" value="Validate" />
      <span asp-validation-for="CaptchaCode"></span>
      @if ((HttpContext.Request.Method == "POST") && ViewData.ModelState.IsValid)
      {
         <span class="correct">Correct!</span>
      }
   </div>
</form>