2011-03-25 86 views
1

我已將recaptcha幫助程序添加到我創建的視圖中。 我的控制器看起來像將recaptcha添加到mvc3中

[HttpPost] 
    public ActionResult Index(ContactModel model) 
    { 
     if (ReCaptcha.Validate(privateKey: ReCaptcha.PublicKey = System.Configuration.ConfigurationManager.AppSettings["RecaptchaKey"])) 
     { 
     } 
    } 

每次驗證失敗,2個問題 1.如何傳遞錯誤返回查看 2.爲什麼當我知道鑰匙沒有返回有效的結果,答案是正確的?

謝謝

+0

尚未在MVC環境中實現ReCaptcha,但這看起來是一個很好的開始參考的地方http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp -net-mvc.aspx它不是MVC3,但我不認爲這會有所作爲 – davidferguson 2011-03-25 21:54:59

回答

3

該代碼不正確設置的私有密鑰(或與此有關的公共密鑰)。在您的控制器中設置您的私鑰,在您的視圖中設置公鑰。

在你的控制器:

<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 

在視圖:

@ ReCaptcha.GetHtml(公鑰:= System.Configuration.ConfigurationManager.AppSettings( 「驗證碼:公鑰」),主題:=」紅「)

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

我的例子是VB.NET,但你應該能夠輕鬆地轉換它。

+0

謝謝埃德的幫助 – 2011-04-22 23:53:37

相關問題