4

Let me start off by saying that I'm using WebMatrix. I'm trying to add a reCAPTCHA plugin to my ASP.NET website. I had a look at the quickstart documentation for their ASP.NET plugin. Here is part of their example:

<%@ Page Language="VB" %>
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
<script runat="server">
    Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        If Page.IsValid Then
            lblResult.Text = "You Got It!"
            lblResult.ForeColor = Drawing.Color.Green
        Else
            lblResult.Text = "Incorrect"
            lblResult.ForeColor = Drawing.Color.Red
        End If
    End Sub
</script>
<html>
  <body>
    <form runat="server">
      <asp:Label Visible=false ID="lblResult" runat="server" />
      <recaptcha:RecaptchaControl
          ID="recaptcha"
          runat="server"
          Theme="red"
          PublicKey="your_public_key"
          PrivateKey="your_private_key"
        />

      <!-- ... -->
    </form>
  </body>
</html>

I know that I wouldn't need the "<%@ Page Language="VB" %>", but I'm still fairly new to Razor, so how would I add a reference to the reCAPTCHA assembly and and display the plugin in my page? I'm doubtful that I could use this line for the assembly reference:

<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

Also, can I put <asp:???> tags and tags from the reCAPTCHA assembly in my CSHTML document? Would this be valid in a WebMatrix website:

<recaptcha:RecaptchaControl
    ID="recaptcha"
    runat="server"
    Theme="red"
    PublicKey="your_public_key"
    PrivateKey="your_private_key"
  />

Basically I'm asking how one would go about adding a reCAPTCHA plugin to a Razor C# file.

Brandon Miller
  • 2,247
  • 8
  • 36
  • 54

4 Answers4

4

There's a control included in the Microsoft.Web.Helpers library. Basic usage is @Microsoft.Web.Helpers.ReCaptcha.GetHtml(publicKey: "...")

Client (razor)

@using (Html.BeginForm("Captcha", "Home", FormMethod.Post))
{
    @Microsoft.Web.Helpers.ReCaptcha.GetHtml(publicKey: "...")    
    <input type="submit" value="submit"/>
}

On the server-side

public ActionResult Captcha()
{
    var valid = Microsoft.Web.Helpers.ReCaptcha.Validate(privateKey: "...");
    if (valid)
    {
        return RedirectToAction("Contact");
    }
    else
    {
        ModelState.AddModelError("Captcha", "Bad input in captcha");
        return View("Index");
    }            
}
Joe
  • 5,389
  • 7
  • 41
  • 63
2

So far none of these work. The OP is using WebMatrix and Razer whereas the examples above/below are for MVC and regular ASP.NET.

The WebMatrix IDE includes a package in NuGet for reCAPTCHA however no instructions on how to use it.

Edit: Here are the instructions for using reCAPTCHA with WebMatrix

http://www.asp.net/web-pages/tutorials/security/using-a-catpcha-to-prevent-automated-programs-%28bots%29-from-using-your-aspnet-web-site

In a nutshell, you need to open NuGet, install the MS Webhelper library and reCAPTCHA. Then add/edit the _AppStart.cshtml as advised and check the example code on the page.

Bob
  • 21
  • 2
0

in Web.config

  <appSettings>
     <add key="webpages:Version" value="1.0.0.0"/>
     <add key="ClientValidationEnabled" value="true"/>
     <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
     <add key="ReCaptchaPrivateKey" value="Your private key here" />
     <add key="ReCaptchaPublicKey" value="Your public key here" />
 </appSettings>

in View Folder\Web.config

  <namespaces>
     <add namespace="System.Web.Mvc" />
     <add namespace="System.Web.Mvc.Ajax" />
     <add namespace="System.Web.Mvc.Html" />
     <add namespace="System.Web.Routing" />
     <add namespace="Recaptcha"/>
  </namespaces>

in cshtml

put this in top

@using Recaptcha;

put this where you want to display

    <div class="editor-label">
        Are you a human?
    </div>
    <div class="editor-field">
        @Html.DisplayFor(model => model.recap_errorRaise)
        @Html.Raw(Html.GenerateCaptcha("captcha", "red"))
        @Html.ValidationMessage("captcha")
    </div>

In Controller

    [HttpPost]
    [RecaptchaControlMvc.CaptchaValidator]
    public ActionResult Index(Home home, bool captchaValid, string captchaErrorMessage)
    {
        if (ModelState.IsValid)
        {
            if (captchaValid)
            {
                //return RedirectToAction("AnswerSecurityQuestion", new { username = model.Username });
                return View(home);
            }
            ModelState.AddModelError("", captchaErrorMessage);
            home.recap_errorRaise = captchaErrorMessage;
        }
        return View(home);
    }
vegas red
  • 259
  • 4
  • 12
0

Put the language value from https://developers.google.com/recaptcha/docs/language in Resource file. example (@Resource.CaptchaLanguage value = 'en')

<script src="https://www.google.com/recaptcha/api.js?hl=@Resource.CaptchaLanguage"></script>