我之前使用过 BotDetect 在 asp 上使用验证码 WEBFORM
,但现在当我尝试将 BotDetect 添加到另一个 Asp WEBFORM
时使用 NuGet 的项目时,我在调试时遇到“对象引用未设置为对象的实例”错误。
我无法加载我的页面,也无法发生任何中断。当它尝试对我的表单页面执行应用程序洞察请求 GET 时,它似乎失败了。然后它在 System.Web.dll 中抛出一个未处理的异常
但是,话虽这么说,当我尝试运行调试器时,我仍然遇到“对象引用未设置为对象的实例”错误,但它引用 BotDetect.Web.UI.WebFormsCaptcha.OnInit(EventArgs e)
我已经删除了 BotDetect 包,重新添加了程序集,尝试从工作项目中添加程序集,并且我尝试从头开始重新创建项目,看看我是否可能配置错误。似乎什么都不起作用。
我目前有BotDetect
和BotDetect.Web.MVC
来自 NuGet 包(版本 4.0.1),我已检查以确保我在 .net 4.5 项目中使用 .net 4.5 版本。
我检查了我的 Packages.config,我可以看到:
<package id="Captcha" version="4.0.1" targetFramework="net452" />
下面是我引用 BotDetect Captcha 的代码。它是一个 MasterPage Web 表单,但验证码驻留在内容占位符中,因为我们在渲染母版后与它进行交互。
我只是不确定我还能做些什么来解决这个问题,有什么建议吗?
ASP 表单页面
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<fieldset>
<legend>ASP.NET WebForm CAPTCHA Validation</legend>
<p class="prompt">
<label for="CaptchaCodeTextBox">Retype the characters from the picture:</label></p>
<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha"
UserInputControlID="CaptchaCodeTextBox" />
<div class="validationDiv">
<asp:TextBox ID="CaptchaCodeTextBox" runat="server"></asp:TextBox>
<asp:Button ID="ValidateCaptchaButton" runat="server" />
<asp:Label ID="CaptchaCorrectLabel" runat="server" CssClass="correct"></asp:Label>
<asp:Label ID="CaptchaIncorrectLabel" runat="server" CssClass="incorrect"></asp:Label>
</div>
</fieldset>
ASP 表单页面后面的代码
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
' initial page setup
If Not IsPostBack Then
' set control text
ValidateCaptchaButton.Text = "Validate"
CaptchaCorrectLabel.Text = "Correct!"
CaptchaIncorrectLabel.Text = "Incorrect!"
' these messages are shown only after validation
CaptchaCorrectLabel.Visible = False
CaptchaIncorrectLabel.Visible = False
End If
If IsPostBack Then
' validate the Captcha to check we're not dealing with a bot
Dim isHuman As Boolean
isHuman = ExampleCaptcha.Validate()
If isHuman Then
CaptchaCorrectLabel.Visible = True
CaptchaIncorrectLabel.Visible = False
Else
CaptchaCorrectLabel.Visible = False
CaptchaIncorrectLabel.Visible = True
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim HF As HiddenField = DirectCast(Page.Master.FindControl("HF1"), HiddenField)
If Not IsPostBack Then
SetInitialRow()
ElseIf HF.Value = "AddNewRow" Then
AddNewRowToGrid()
Else
btnSubmit_Click()
End If
Done:
End Sub
最佳答案
因此,我与 BotDetect 的开发人员进行了交谈,结果发现这实际上是他们当前版本的一个错误。
目前,您无法使用 BotDetect (v4.0.1) 在内容页(带有母版页的 Web 表单)中设置 UserInputControlID 属性。下一个版本中将会有一个补丁。
同时,根据我发布的代码,您可以在代码隐藏(VB)中以编程方式设置它。
CODE BEHIND (in page_prerender)
ExampleCaptcha.UserInputID = CaptchaCodeTextBox.ClientID
您可能还需要更换
isHuman = ExampleCaptcha.Validate()
与
isHuman = ExampleCaptcha.Validate(CaptchaCodeTextBox.Text.Trim())
CONTENT WEB FORM
<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha" />
关于asp.net - 使用 BotDetect 验证码时为 "Object reference not set to an instance of an object.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37843928/