简体   繁体   English

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

[英]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. 我们在ASP.NET项目中使用了reCAPTCHA,并将验证码从ascx发送到页面(服务器端)后面的代码没有问题。 However, after March 2018, we had to renew the captcha to v2 and make the following changes : 但是,在2018年3月之后,我们必须将验证码更新为v2并进行以下更改:

User Control (cs): 用户控件(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. 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): 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. 发送此表单时,我们遇到错误pge,并且相关日志中存在以下错误。 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". 另一方面,当我们查看Firebug网络时,即使我们使用具有name =“ captcha”的“ name”属性,也观察到g-recaptcha-response ,但未观察到验证码参数。

Exception information: 异常信息:

Exception type: NullReferenceException Exception message: Object reference not set to an instance of an object. 异常类型:NullReferenceException异常消息:对象引用未设置为对象的实例。 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) 在System.Web.UI的Tools.WebUI._ContactForm.btnSend_Click(对象发件人,EventArgs e)(在System.Web.UI.WebControls.Button.RaisePostBackEvent(字符串eventArgument)处)在Tools.Domain.FormUserControl.Submit(对象发件人)处。 Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)

Any idea? 任何想法?

I followed this great tutorial to implement reCaptcha v2 in ASP.NET WebForms: 我遵循了这个很棒的教程,以在ASP.NET WebForms中实现reCaptcha v2:

https://www.aspsnippets.com/Articles/Google-RECaptcha-Code-with-Example-in-ASPNet.aspx 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. PS:这个人有很多很棒的技巧和文章,我会不时检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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