How to set recaptcha key settings at runtime How to set recaptcha key settings at runtime asp.net asp.net

How to set recaptcha key settings at runtime


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.


Another way to set key values, use <appSettings> keys RecaptchaPublicKey and RecaptchaPrivateKey. These values will be used automatically unless it is overridden during control declaration (mjd79's answer, first way).

Pro: if you have multiple declaration, you only need to keep the keys in one place, DRY principle.

This behaviour can be seen through the source code, RecaptchaControl.cs, line 135-...:

public RecaptchaControl(){    this.publicKey = ConfigurationManager.AppSettings["RecaptchaPublicKey"];    this.privateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"];    ...