How to Set Recpatcha Key in Web.config File

As you may have already seen, you can directly assign public and private keys to the respective properties of Recpatcha ASP.NET control or Recaptcha MVC HTML extension. However, a better way is to store these keys in your web.config file. The obvious benefit is that you can change these keys anytime you want without requiring you to modify your code and perhaps most important benefit is that you the keys you define in your web.config are global in your web project.

In the appSettings section of your web.config file, add the keys as follows:

<add name="recaptchaPublicKey" value="Your Public Key" />
<add name="recaptchaPrivateKey" value="Your Private Key" />

Note: The appSettings keys are automatically added to your web.config file if you install Recaptcha for .NET through Nuget. However, you would still need to provide your own public and private keys in the web.config file of your project.

See Also

Last edited Feb 4, 2013 at 5:01 AM by tanveery, version 7


phoenix20_06 Nov 11, 2014 at 2:32 PM 
As @GrumpyWookie said the setting is <add key... and not add name...
Also, if you added ReCaptcha using NuGet, the installation will add <add name="recaptchaPublicKey" value=" " /> and <add name="recaptchaPrivateKey" value=" " /> to your appSettings automatically. It will be the last entry.

avaleri Mar 4, 2014 at 11:57 PM 
I was getting the same publicKey cannot be null error, and had to use the following snippet to get it to read the publickey from the web.config file:

<cc1:Recaptcha ID="Recaptcha1" PublicKey="{reCaptcha:publicKey}" PrivateKey="{reCaptcha:privateKey}" runat="server" />

<add key="reCaptcha:publicKey" value="My Public Key" />
<add key="reCaptcha:privateKey" value="My Private Key" />

mikedonner Mar 4, 2014 at 10:05 PM 
I get an error stating the publicKey cannot be null or empty when I use the web.config version using the 'key' instead of 'name' (although I get the same error if I use 'name' too). Works fine as an attribute in @Html.Recaptcha(). Using MVC 5 with EntityFramework 6 on VS 2013.

GrumpyWookie Jul 30, 2013 at 4:37 AM 
The settings above should be "KEY" not "NAME"...

<add [[KEY]]="recaptchaPublicKey" value="Your Public Key" />
<add [[KEY]]="recaptchaPrivateKey" value="Your Private Key" />

** Have included square brackets to highlight - should be 'lowercase' without brackets.