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

时间:2018-10-09 15:24:59

标签: asp.net button asp.net-mvc-5 recaptcha captcha

我有一个名为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>

当前,验证码使用图像进行验证,因此,如果我检查了正确的图像,则验证码将是有效的,而且如果我单击提交(“加入IoT实验室”)而不使用验证码,它将仍然重定向我...

那么,如何在验证验证码后启用Join IoT Lab!(提交)按钮?我的意思是,如何将Recaptcha设置为必填字段?

我想我应该使用类似ViewBag的东西,但是怎么用?

1 个答案:

答案 0 :(得分:0)

您可以在“加入IoT实验室!”中检查viewBag.Message条件。按钮。此消息 在表单提交后启用。也许我了解您的问题并给出正确的解决方案。如果您有任何问题,请了解我。

controller do
  def update
    resource = Organization.find(params[:id])
    former_ids = resource.collection_ids
    super
    new_ids = resource.reload.collection_ids
    # my logic here
  end
end