How do I add a CAPTCHA to a web site written with ASP.NET/VB

Robert Barnes 71 Reputation points
2024-04-27T05:08:50.44+00:00

A bot seems to be using my web site to send out emails via AmazonSES. I want to add a CAPTCHA to it to prevent this. I came across

https://stackoverflow.com/questions/71515717/how-to-add-captcha-in-asp-net-project

which appears to be what I want, but the examples use C# rather than VB. I tried to convert the C# to VB, but I got into a mess.

I checked the similar questions through this forum and found this

https://learn.microsoft.com/en-us/answers/questions/652050/vb-human-verificaion-boot-attacks-recaptcha-or-sim

which tells me that reCAPTCHA is not supported on this forum. So is there something that works with ASP/NET (VB)?

Any useful information will be appreciated.

Thank you, Robert.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,323 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,610 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 26,686 Reputation points Microsoft Vendor
    2024-04-29T03:45:15.3233333+00:00

    Hi @Robert Barnes,

    You can use the MSCaptcha Library in ASP.Net.

    Download MSCaptcha

    In order to use MSCaptcha control, you will need to add reference of MSCaptcha Library and then register on the Page as shown below.

    <%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>

    Web.Config Modifications:

    <configuration>
        //ADD
    	<appSettings>
    		<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    	</appSettings>
      <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.8" />
        //ADD
    	  <httpHandlers>
    		  <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
    	  </httpHandlers>
        <httpRuntime targetFramework="4.8" />
      </system.web>
      <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
      </system.codedom>
      //ADD
    	<system.webServer>
    		<validation validateIntegratedModeConfiguration="false"/>
    		<handlers>
    			<add name="MSCaptcha.captchaImageHandler" verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha" resourceType="Unspecified"/>
    		</handlers>
    	</system.webServer>
    </configuration>
    

    Demo

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <%@ Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <cc1:CaptchaControl ID="captcha1" runat="server" CaptchaBackgroundNoise="Low" CaptchaLength="5"
                    CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5"
                    CaptchaMaxTimeout="240" FontColor="#529E00" ErrorInputTooFast="Image text was typed too quickly. " ErrorInputTooSlow="Image text was typed too slowly." EnableViewState="False" />
                <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvCaptcha" runat="server" ErrorMessage="*Required"
                    ControlToValidate="txtCaptcha" Display="Dynamic"></asp:RequiredFieldValidator>
                <asp:Label ID="lblMessage" runat="server"></asp:Label>
                <br />
                <br />
                <asp:Button ID="btnVerify" runat="server" Text="Verify" OnClick="OnVerify" />
            </div>
        </form>
    </body>
    </html>
    
    Imports System.Drawing
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub OnVerify(sender As Object, e As System.EventArgs)
            captcha1.ValidateCaptcha(txtCaptcha.Text.Trim())
            If captcha1.UserValidated Then
                lblMessage.ForeColor = Color.Green
                lblMessage.Text = "Valid"
            Else
                lblMessage.ForeColor = Color.Red
                lblMessage.Text = "InValid"
            End If
        End Sub
    End Class
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.