2011-03-25 9 views
1

작성한 뷰에 recaptcha 도우미를 추가했습니다. 내 컨트롤러는 유효성 검증 실패mvc3에 recaptcha 추가

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

매번처럼이 질문에 보이는 1. 나는 2. 왜 키를 알고하지 않을 때 유효한 반환 된 결과입니다 볼 수 다시 오류를 통과하려면 어떻게해야하고 대답은 정확합니까?

당신에게

+0

아직 MVC 환경에서 ReCaptcha가 구현되지 않았지만 참조 용으로 시작하는 것이 좋습니다. http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp -net-mvc.aspx MVC3이 아니지만 차이가 있다고 생각해서는 안됩니다. – davidferguson

답변

3

그 코드가 제대로 개인 키 (또는 그 문제에 대한 공개 키)를 설정하지 않습니다 감사합니다. 컨트롤러에서 개인 키를 설정하고 공개 키를 설정하십시오.

ReCaptcha.GetHtml (공개 키 @ : =의 System.Configuration.ConfigurationManager.AppSettings ("reCAPTCHA를 : 공개 키"), 테마 : = "보기에

<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 

: 컨트롤러에서

빨간색 ")의 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

에드 감사합니다. –