Search code examples
c#asp.netdotnetnukerecaptcha

Recaptcha doesn't load dynamically in DNN module using Recaptcha.dll


I've a problem to load Google Recaptcha control when I try to assign Public & Private Keys dynamically in code behind. It works fine in simple ASP.Net website but It doesn't work when I integrate it in DNN module.

Error Message: reCAPTCHA needs to be configured with a public & private key.

Stack trace:

DotNetNuke.Services.Exceptions.PageLoadException: reCAPTCHA needs to be configured with a public & private key. ---> System.ApplicationException: reCAPTCHA needs to be configured with a public & private key. at Recaptcha.RecaptchaControl.OnInit(EventArgs e) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at System.Web.UI.ControlCollection.Add(Control child) at DotNetNuke.UI.Modules.ModuleHost.InjectModuleContent(Control content) at DotNetNuke.UI.Modules.ModuleHost.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace ---

Markup:

<googleCaptcha:RecaptchaControl ID="ctlRecaptcha" runat="server" />

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    ctlRecaptcha.PublicKey = "xyz";
    ctlRecaptcha.PrivateKey = "abc";
}

Solution

  • I actually have a tutorial on DNNHero.com that teaches on creating a custom DNN registration with the Google Recaptcha. It includes a video and code project.

    Looking at my code, I included the attributes for publickey and privatekey on my ascx, but with dummy values:

    <googleCaptcha:RecaptchaControl ID="ctlRecaptcha" runat="server" PublicKey="your_public_key" PrivateKey="your_private_key"/>
    

    I set the attribute values like you did in my Page_Load event. This worked for me. Give it a try and comment if it still throw the error.