简体   繁体   中英

Cannot Integrate reCAPTCHA v2 (server side) in ASP.NET

We have used reCAPTCHA in our ASP.NET project and send the captcha from ascx to code behind page (server side) without no problem. However, after March 2018, we had to renew the captcha to v2 and make the following changes :

User Control (cs):

....
protected Recaptcha.Web.UI.Controls.Recaptcha Captcha;
protected override Recaptcha.Web.UI.Controls.Recaptcha ReCaptcha 
    { get { return this.Captcha; } }

protected void btnSend_Click(object sender, EventArgs e)
{
    if (this.Submit(sender))
        this.mvMain.ActiveViewIndex++;
}


reCAPTCHA v1 (ascx): In this version Site key is defined in web.config.

<%@ Control Language="C#" AutoEventWireup="true" Inherits="Tools.WebUI._ContactForm"%>

<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1"%>

<asp:MultiView ID="mvMain" ActiveViewIndex="0" runat="server">
    <asp:View runat="server">
        <div id="divCaptcha">

            <!-- code omitted for brevity -->

            <div id="captcha">
                <cc1:Recaptcha ID="Captcha" Theme="Clean" runat="server" />

            </div>
            <asp:TextBox ID="txtBotCheck" style="display:none !important" runat="server" />
            <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" />
            <asp:Label ID="lblMessage" runat="server" CssClass="captchaMessage"></asp:Label>
        </div>
    </asp:View>
    <asp:View runat="server">
        Message was sent...
    </asp:View>
</asp:MultiView>


reCAPTCHA v2 (ascx):

<%@ Control Language="C#" AutoEventWireup="true" Inherits="Crema.Tools.WebUI._ContactForm"%>

<%@ Register Assembly="Recaptcha.Web" Namespace="Recaptcha.Web.UI.Controls" TagPrefix="cc1"%>

<asp:MultiView ID="mvMain" ActiveViewIndex="0" runat="server">
    <asp:View runat="server">

    <script src="http://www.google.com/recaptcha/api.js" async defer></script> 

        <div id="divCaptcha">

            <!-- code omitted for brevity -->

            <div id="captcha">
                <div class="g-recaptcha" data-sitekey="tlcUAAACV...............FhQ"></div>     
            </div>

            <asp:TextBox ID="txtBotCheck" style="display:none !important" runat="server" />
            <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" />
            <asp:Label ID="lblMessage" runat="server" CssClass="captchaMessage"></asp:Label>
        </div>
    </asp:View>
    <asp:View runat="server">
        Message was sent...
    </asp:View>
</asp:MultiView>

When sending this form we have encounter error pge and there is the following error in the related logs. On the other hand when we look at the Firebug Network, we observed g-recaptcha-response , but not observed captcha paremeter even if we use "name" property with name="captcha".

Exception information:

Exception type: NullReferenceException Exception message: Object reference not set to an instance of an object. at Tools.Domain.FormUserControl.Submit(Object sender) at Tools.WebUI._ContactForm.btnSend_Click(Object sender, EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Any idea?

I followed this great tutorial to implement reCaptcha v2 in ASP.NET WebForms:

https://www.aspsnippets.com/Articles/Google-RECaptcha-Code-with-Example-in-ASPNet.aspx

PS: This guy has a lot of excellent tips and articles that I check time to time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM