无法在ASP.NET中集成reCAPTCHA v2(服务器端)

时间:2018-05-07 13:52:12

标签: javascript c# asp.net recaptcha captcha

我们在ASP.NET项目中使用了reCAPTCHA,并将ascx的验证码发送到页面后面的代码(服务器端),没有任何问题。但是,在2018年3月之后,我们不得不将验证码更新为v2并进行以下更改:

用户控制(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): 在此版本中,网站密钥在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>

发送此表单时,我们遇到错误pge,相关日志中出现以下错误。另一方面,当我们查看Firebug网络时,我们观察到 g-recaptcha-response ,但是即使我们使用&#34; name&#34;属性名称为&#34;验证码&#34;。

例外信息:

  

异常类型:NullReferenceException   异常消息:未将对象引用设置为对象的实例。   在Tools.Domain.FormUserControl.Submit(对象发送者)   在Tools.WebUI._ContactForm.btnSend_Click(Object sender,EventArgs e)   在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)   在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我按照这个很棒的教程在ASP.NET WebForms中实现了reCaptcha v2:

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

P.S。:这家伙有很多优秀的技巧和文章,我不时检查。