代码之家  ›  专栏  ›  技术社区  ›  Neo Cortex

如果recaptcha有效,如何启用按钮

  •  0
  • Neo Cortex  · 技术社区  · 6 年前

    我叫控制器 AccountController 使用recaptcha和如下代码:

    [HttpPost]
            public ActionResult FormSubmit()
            {
    
                var response = Request["g-recaptcha-response"];
                string secretKey = "6Lf_NnQUAAA......";  //Added my secret key that genereted on my re-captcha
                var client = new WebClient();
                var result = client.DownloadString(string.Format("https://google.com/recaptcha/api/siteverify?secret={0}&response{1}", secretKey, response));
                var obj = JObject.Parse(result);
                var status = (bool)obj.SelectToken("success");
                ViewBag.Message = status ? "successs" : "failed";
    
                return View();
            }
    

    还有,我有 LogIn.cshtml 使用以下代码查看:

    <div>
        @using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
        {
            <div class="g-recaptcha" data-sitekey="6Lf_NnQUAAA..."></div> //ADDED MY SITE KEY
            <input type="submit" name="commit" value="Join IoT Lab!">  
        }
    </div>
    <span style="display:inline-block; font-size:20px;margin:20px 0;padding:20px;border:1px solid #D3D3D3">
        @ViewBag.Message
    </span>
    <p>
        By clicking "Join IoT Lab", you agree to our <br />
        <a href="@Url.Action("PrivacyRulesAndGuidelines", "IOTLabProject")">Terms and conditions</a> and <a href="@Url.Action("PrivacyRulesAndGuidelines", "IOTLabProject")">Privacy policy</a>
    </p>
    
    <script src='https://www.google.com/recaptcha/api.js' type="text/javascript"></script>
    

    目前,验证码使用图像进行验证,所以如果我检查正确的图像,验证码将有效,但如果我只是单击提交(“加入物联网实验室”)而不使用验证码,它仍然会重定向我…

    那么,如何启用 Join IoT Lab! (commit)按钮在验证recaptcha之后?我是说,如何将recaptcha设置为必需字段?

    我想我应该用像 ViewBag 但是怎么办呢?

    1 回复  |  直到 6 年前
        1
  •  0
  •   zaman    6 年前

    您可以查看viewbag.message条件“加入物联网实验室!”按钮。此消息 表单提交后启用。可能我理解您的问题并给出正确的解决方案。如果您有任何问题,请了解我。

    <div>
    @using (Html.BeginForm("FormSubmit", "Home", FormMethod.Post))
    {
        <div class="g-recaptcha" data-sitekey="6Lf_NnQUAAA..."></div> //ADDED MY SITE KEY
        <input type="submit" name="commit" value="Join IoT Lab!">  
    }
    </div>
    <span style="display:inline-block; font-size:20px;margin:20px 
    0;padding:20px;border:1px solid #D3D3D3">
    @ViewBag.Message
    </span>
    <p>
    @if(ViewBag.Message=="successs")
    {
      By clicking "Join IoT Lab", you agree to our <br />
      <a href="@Url.Action("PrivacyRulesAndGuidelines", "IOTLabProject")">Terms and 
       conditions</a> and <a href="@Url.Action("PrivacyRulesAndGuidelines", 
       "IOTLabProject")">Privacy policy</a>
    }
    
     </p>
    
    <script src='https://www.google.com/recaptcha/api.js' type="text/javascript"> 
    </script>