expenguin expenguin - 5 months ago 29
Vb.net Question

"Object reference not set to an instance of an object." when using BotDetect Captcha

I've used BotDetect before to utilize a captcha on an asp

WEBFORM
, but now when I try to add BotDetect to another Asp
WEBFORM
project using NuGet, I'm greeted with an 'Object Reference not set to an instance of an object' error upon debugging.

I cannot load my page nor get any breaks to occur. It seems to be failing when it tries to do an Application insights request GET of my form page. It then throws an unhanded exception in System.Web.dll

However, that being said, when I try to run he debugger, I'm still met with a 'Object Reference not set to an instance of an object' error, but it references
BotDetect.Web.UI.WebFormsCaptcha.OnInit(EventArgs e)


I've removed the BotDetect package, re-added the assemblies, tried to add the assemblies from the working project, and I've tried to recreate the project from the ground up to see if I maybe had mis-configured something. Nothing seems to work.

I currently have
BotDetect
and
BotDetect.Web.MVC
from the NuGet package (version 4.0.1) and I've checked to make sure I'm using the .net 4.5 versions with my .net 4.5 project.

I've checked my Packages.config and I can see:

<package id="Captcha" version="4.0.1" targetFramework="net452" />


Below is my code that references the BotDetect Captcha. It is a MasterPage web form, but the Captcha resides in a content placeholder since we interact with it after the master has been rendered.

I'm just not sure what else I can do to fix this, any suggestions?

ASP Form Page

<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 Form Page CODE BEHIND

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

Answer

So I've since spoken to the devs of BotDetect and it turned out that this is in fact a bug with their current release.

You currently cannot set the UserInputControlID property in a content page (Web form with master page) with BotDetect (v4.0.1). There will be a patch for this in the next version.

In the meantime, you can set it programmatically in the codebehind (VB).

CODE BEHIND (in page_prerender)

ExampleCaptcha.UserInputID = CaptchaCodeTextBox.ClientID

CONTENT WEB FORM

<BotDetect:WebFormsCaptcha runat="server" ID="ExampleCaptcha" />