Google reCAPTCHA dll

Google reCAPTCHA dll

我正在尝试将 google reCAPTCHA 与我网站的注册表单集成。我找到了 a solution at CodeProject. 我已经下载了 dll 并将其添加到项目 Reference 文件夹中。

以下是一些代码片段:

html:

<asp:CreateUserWizard ID="CreateUserWizard2" OnCreatingUser="CreateUserWizard2_CreatingUser" OnCreateUserError="CreateUserWizard2_CreateUserError" oncreateduser="CreateUserWizard2_CreatedUser" ContinueDestinationPageUrl="/Account/MyDashboard.aspx" ContinueButtonStyle-CssClass="btn btn-default col-md-3 btn-search input-password" runat="server" RequireEmail="false" Width="100%" RenderOuterTable="false" QuestionRequiredErrorMessage="false">
    <WizardSteps>
        <asp:CreateUserWizardStep ID="CreateUserWizardStep2" runat="server" >
            <ContentTemplate>
                <h5 class="text-center">Please complete the following fields to register for an account</h5>
                <br />
                <h5 class="right">Login Details</h5>
                <br/>
                <asp:TextBox ID="UserName" runat="server" class="form-control height-fix" placeholder="EMAIL" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" TextMode="Email" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ForeColor="red" ValidationGroup="CreateUserWizard2" />

                <asp:TextBox ID="Password" runat="server" class="form-control height-fix" placeholder="PASSWORD" TextMode="Password"  />
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ForeColor="red" ValidationGroup="CreateUserWizard2" />

                <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password" class="form-control height-fix" placeholder="REPEAT PASSWORD" />
                    <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword" ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required." ForeColor="red" ValidationGroup="CreateUserWizard2" />
    <cc1:GoogleReCaptcha ID="ctrlGoogleReCaptcha" runat="server" PublicKey="keyinhere" PrivateKey="secretinheree"  />
                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="false" />

            </ContentTemplate>
            <CustomNavigationTemplate>
                <asp:Button ID="StepNextButton" runat="server" class="btn btn-md btn-sign-in btn-block btn-padding" CommandName="MoveNext" Text="SUBMIT" ValidationGroup="CreateUserWizard2" />
            </CustomNavigationTemplate>

        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep2" runat="server" AllowReturn="true" />
    </WizardSteps>
</asp:CreateUserWizard>

C#:

...
using GoogleReCaptcha;

namespace WebOnline
{
    public partial class PublicAccess : System.Web.UI.MasterPage
    {
        private NLog.Logger _log = NLog.LogManager.GetCurrentClassLogger();

        protected void CreateUserWizard2_CreatedUser(object sender, EventArgs e)
        {
            // Validation code block goes in here.
        }

        [Import]
        IUnitOfWork IUnitOfWork;

        private UnitOfWork _UnitOfWork;
        private UnitOfWork UnitOfWork
        {
            get
            {
                if (_UnitOfWork == null)
                {
                    _UnitOfWork = IUnitOfWork as UnitOfWork;
                }
                return _UnitOfWork;
            }
        }
        private MainMenuPages _currentPage;
        public MainMenuPages CurrentPage
        {
            get
            {
                return _currentPage;
            }
            set
            {
                _currentPage = value;
            }
        }

        protected string GetCurrentPageStyle(string pageName)
        {
            if (CurrentPage.ToString() == pageName)
            {
                return "active-main-li";
            }
            else
            {
                return "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack)
            {
                return;
            }
        }
    }
}

我可以让 reCAPTCHA 出现在表单中,但尝试验证 CAPTCHA 时出现问题。说明建议输入以下代码

if (ctrlGoogleReCaptcha.Validate())
{
   //submit form success
   lblStatus.Text = "Success";
}
else
{
    //captcha challenge failed
    lblStatus.Text = "Captcha Failed!! Please try again!!";
}

但是我的代码不喜欢 ctrlGoogleReCaptcha.Validate() 方法。它说该名称在当前上下文中不存在。我不是通过 runat="server" 发送的吗?我不确定为什么找不到它。

您没有声明 google 验证码方法。请在 PublicAccess class.

中添加以下行
GoogleReCaptcha.GoogleReCaptcha ctrlGoogleReCaptcha = new GoogleReCaptcha.GoogleReCaptcha();