Frage

I am trying to put ReCaptcha in registration form (ASP.NET MVC).

@Html.Raw(Html.GenerateCaptcha()) 

I have entered private and public keys from ReCaptcha into Web.config:

<appSettings>
    <add key="ReCaptchaPrivateKey" value="6LcMwPESAAAAAFyxyxyxyxyxyxy"/>
    <add key="ReCaptchaPublicKey" value="6LcMwPESAAAAAGVyxyxyxyxyxyxy"/>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

But it is giving this error:

Input error: k: Format of site key was invalid

How to solve this problem?

PS. I have read CoffeeCup solution here. But could not solve problem in that way in ASP.NET.

War es hilfreich?

Lösung

Recaptcha with ASP.NET MVC can very easily be implemented under a minute using this nuget extension - Recaptcha for .NET

I was able to set up one and have blogged about the same here - Implementing reCAPTCHA in your ASP.NET MVC Project

Below is a summary of the same....

First you register and get your public and private key. (this step you seem to have already done with)

Install "Recaptcha for .NET" using NuGet Package Manager, Make sure you download the one shown in the screenshot below as there were plenty others with the same name. http://yassershaikh.com/wp-content/uploads/2014/04/step2-recaptcha-nuget-aspnetmvc.png

Add the Recaptcha Control to Your MVC View

Open your Views/Account/Register view and add this to the top of the page

@using Recaptcha.Web.Mvc

and include the recaptcha form using the following razor code

<li>
    @Html.Label("Some label to go here")
    @Html.Recaptcha()
</li>

Verify User's Response to Recaptcha Challenge in your Controller/Action

Next step is to configure recaptcha in your controller/action, start with importing the following namespaces in your controller file (AccountController for this example)

using Recaptcha.Web;
using Recaptcha.Web.Mvc;

Next, go to your Register method and use the following code

RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper();

if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
    ModelState.AddModelError("", "Captcha answer cannot be empty.");
    return View(model);
}

RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();

if (recaptchaResult != RecaptchaVerificationResult.Success)
{
    ModelState.AddModelError("", "Incorrect captcha answer.");
}

Here is how the recaptcha form looks on my register page where I setup using the above code.

enter image description here

Hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top