Reputation: 93
I am trying to integrate google reCAPTCHA with a registration form for my website. I have found a solution at CodeProject. I have downloaded the dll and added it to the project Reference folder.
Here are some code snippets:
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;
}
}
}
}
I can get the reCAPTCHA to appear in the form, but trying to validate the CAPTCHA is causing problems. The instructions suggest entering the following code
if (ctrlGoogleReCaptcha.Validate())
{
//submit form success
lblStatus.Text = "Success";
}
else
{
//captcha challenge failed
lblStatus.Text = "Captcha Failed!! Please try again!!";
}
However my code doesn't like the ctrlGoogleReCaptcha.Validate()
method. It says that the name doesn't exist in the current context. Have I not sent this through the runat="server"
though? I'm not sure why it's not finding it.
Upvotes: 0
Views: 2792
Reputation: 520
You did not declare google Captcha method. Please add below line in PublicAccess
class.
GoogleReCaptcha.GoogleReCaptcha ctrlGoogleReCaptcha = new GoogleReCaptcha.GoogleReCaptcha();
Upvotes: 1