2013-02-20 82 views
3

讓我開始說我正在使用WebMatrix。我正在嘗試向我的ASP.NET網站添加一個reCAPTCHA插件。我查看了他們的ASP.NET插件的quickstart文檔。這裏是他們的一部分。例如:如何將reCAPTCHA ASP.NET插件放入Razor(CSHTML)文件中?

<%@ 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> 

我知道,我不會需要「<%@頁面語言=」 VB「%>」,但我仍然相當新的剃鬚刀,所以怎麼會我添加了對reCAPTCHA程序集的引用,並在我的頁面中顯示插件?我懷疑,我可以用這條線作爲組件參照:

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

而且,我可以把<asp:???>標記和標籤從驗證碼總成我CSHTML文件?這將是在WebMatrix的網站有效:

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

基本上我問一個將如何去增加一個驗證碼插件剃刀C#文件。

+0

值得一提的是[無插件顯示reCAPTCHA](https://developers.google.com/recaptcha/docs/display)。 – MikeSmithDev 2013-02-20 19:29:38

+0

或...一個爲MVC設計的插件(可能更容易找到然後一個網頁實際上形成一個) – 2013-02-20 19:43:02

+1

對於MVC,你可以這樣做:http://mvcrecaptcha.codeplex.com/但是你正在使用Razor的網頁,對嗎? – 2013-02-20 19:57:37

回答

4

Microsoft.Web.Helpers庫中包含一個控件。基本用法是@Microsoft.Web.Helpers.ReCaptcha.GetHtml(publicKey: "...")

客戶端(剃刀)

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

在服務器端

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"); 
    }    
} 
+0

如何使用此方法驗證reCAPTCHA? – 2013-02-21 05:57:40

0

在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> 

在查看文件夾\ 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> 

在CSHTML

要顯示

<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> 

在控制器

[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); 
    } 
+0

他說他沒有使用MVC或Webform,而是「網頁」。你的答案是MVC。 – 2016-06-22 15:34:59

2

到目前爲止沒有這些工作把這個頂

@using Recaptcha; 

把這個。 OP使用WebMatrix和Razer,而上面/下面的例子是針對MVC和常規ASP.NET的。

WebMatrix IDE包含NuGet中的reCAPTCHA包,但沒有關於如何使用它的說明。

編輯:以下是使用驗證碼與WebMatrix的

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

簡而言之說明書,你需要打開的NuGet,安裝MS Webhelper庫和驗證碼。然後按照建議添加/編輯_AppStart.cshtml並檢查頁面上的示例代碼。

相關問題