让我首先说我正在使用WebMatrix。我正在尝试将reCAPTCHA插件添加到我的ASP.NET网站。我看了他们的ASP.NET插件的快速启动文档。以下是他们示例的一部分:
<%@ 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>
我知道我不需要"<%@Page Language="VB"%>",但我对Razor还很陌生,那么我该如何添加对reCAPTCHA程序集的引用并在我的页面中显示插件呢?我怀疑我是否可以使用这条线作为组装参考:
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>
此外,我可以将<asp:???>
标记和reCAPTCHA程序集中的标记放在CSHTML文档中吗?这在WebMatrix网站上有效吗:
<recaptcha:RecaptchaControl
ID="recaptcha"
runat="server"
Theme="red"
PublicKey="your_public_key"
PrivateKey="your_private_key"
/>
基本上,我在问如何在Razor C#文件中添加reCAPTCHA插件。
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");
}
}
到目前为止,这些都不起作用。OP使用WebMatrix和Razer,而上面/下面的示例是MVC和常规ASP.NET
WebMatrix IDE在NuGet中包含一个用于reCAPTCHA的包,但没有关于如何使用它的说明
编辑:以下是在WebMatrix 中使用reCAPTCHA的说明
http://www.asp.net/web-pages/tutorials/security/using-a-catpcha-to-prevent-automated-programs-%28bots%29-从使用您的aspnet网站
简而言之,您需要打开NuGet,安装MSWebhelper库并重新CAPTCHA。然后根据建议添加/编辑_AppStart.cshtml,并检查页面上的示例代码。
<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 中
把这个放在的顶部
@using Recaptcha;
把这个放在你想显示的地方
<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);
}
将语言值从https://developers.google.com/recaptcha/docs/language在资源文件中。示例(@Resource.CaptchaLanguage值='en')
<script src="https://www.google.com/recaptcha/api.js?hl=@Resource.CaptchaLanguage"></script>