I have added the recaptcha helper to a view I have created. My controller looks like
[HttpPost]
public ActionResult Index(ContactModel model)
{
if (ReCaptcha.Validate(privateKey: ReCaptcha.PublicKey = Sys开发者_JAVA技巧tem.Configuration.ConfigurationManager.AppSettings["RecaptchaKey"]))
{
}
}
Everytime the validation is failing, 2 questions 1. How do I pass the error back to view 2. Why is the result not been returned as valid when I know the key and the answer is correct?
Thank you
That code is not setting the private key correctly (or the public key for that matter). Set your private key in your controller, set the public key in your view.
In your controller:
<HttpPost()> _
Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult
If Microsoft.Web.Helpers.ReCaptcha.Validate(privateKey:=System.Configuration.ConfigurationManager.AppSettings("reCaptcha:privateKey")) Then
If ModelState.IsValid Then
If MembershipService.ValidateUser(model.UserName, model.Password) Then
FormsService.SignIn(model.UserName, model.RememberMe)
If Not String.IsNullOrEmpty(returnUrl) AndAlso Url.IsLocalUrl(returnUrl) Then
Return Redirect(returnUrl)
Else
Return RedirectToAction("Index", "Home")
End If
Else
ModelState.AddModelError("", "The user name or password provided is incorrect.")
End If
End If
Else
ModelState.AddModelError("", "The reCaptcha is incorrect!!")
End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function
In your view:
@ReCaptcha.GetHtml(publicKey:=System.Configuration.ConfigurationManager.AppSettings("reCaptcha:publicKey"), theme:="red")
In web.config:
<appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="reCaptcha:privateKey" value="6LceccMSAAAAAFi54eiELTCDUwrHjY-Qwot05Kip" />
<add key ="reCaptcha:publicKey" value="6LceccMSAAAAAO9u14kcJflcpGD6XnfjNf1KcKz6" />
</appSettings>
My example is in VB.NET, but you should be able to convert it easily.
精彩评论