【问题标题】:BotDetect and ASPNET Razor Pages is not validatingBotDetect 和 ASPNET Razor 页面未验证
【发布时间】:2021-11-08 01:30:03
【问题描述】:

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

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

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

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

挖掘属性的源码,我发现了这个

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

但是,当我直接在 OnPost 方法中尝试该代码时,mvcCaptch.IsSolved 总是返回 false。

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

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

【问题讨论】:

  • 您能告诉我们您使用的是哪个版本的 Asp.net 核心版本吗?从文档中我们可以发现它支持ASP.NET MVC Core 1/2,但不确定它是否支持Asp.net core 3+,是否支持剃须刀页面应用程序?
  • 最新版本,3.1。我只是认为会有一些方法来验证验证码。确定有数据吗?

标签: asp.net-core razor-pages botdetect


【解决方案1】:

我发现有一个属性 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>

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2021-02-22
    相关资源
    最近更新 更多