Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set recaptcha key settings at runtime

I'm implementing the recaptcha control from google.

I built a simple c# test project from their example and all works. Now, instead of having the PublicKey and PrivateKey in the aspx page, I'd rather assign these values at run time as they will most likely be pulled from either the web.config or a database table.

I tried the following in the Page_Load

    protected void Page_Load(object sender, EventArgs e) {
        recaptcha.PublicKey = "<deleted for obvious reasons>";
        recaptcha.PrivateKey = "<ditto>";
    }

but I get an error stating "reCAPTCHA needs to be configured with a public & private key."

I also tried overriding the oninit method of the page and assigning the values there, but no joy.

Any ideas on where this needs to go?

like image 281
NotMe Avatar asked Jun 25 '10 15:06

NotMe


1 Answers

Try using the value of setincodebehind in your tag, like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="setincodebehind" PrivateKey="setincodebehind" ... />

That should let you set the keys in the codebehind properly. There are a couple of other ways to do it as well. For example, you can get the values from a static class like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%= RecaptchaSettings.PublicKey %>" 
  PrivateKey="<%= RecaptchaSettings.PrivateKey %>" ... />

Where RecaptchaSettings is a class you provide. Or, you could put the keys into an appSettings section of your web.config, and access them like this:

<recaptcha:RecaptchaControl ID="myRecaptcha" runat="server" 
  PublicKey="<%$appSettings:RecaptchaPublicKey %>" 
  PrivateKey="<%$appSettings:RecaptchaPrivateKey %>" ... />

Hope that helps.

like image 160
matt Avatar answered Sep 24 '22 03:09

matt