مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

soudabeh

عضویت از 1395/01/12

مشکل در استفاده از reCaptcha گوگل

  • دوشنبه 22 آبان 1396
  • 13:55
تشکر میکنم

با سلام

 من قصد دارم از reCaptcha گوگل در پروژه ام استفاده کنم.

این partial view من هست:

@model VaraminNameh.Areas.Admin.Models.Comment

@using (Html.BeginForm("SaveComment", "NewsList", FormMethod.Post, new {@id="myform" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.HiddenFor(model => model.NewsListID)
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2 col-xs-12" })
            <div class="col-md-10 col-xs-12">
                @Html.TextAreaFor(model => model.Description, new { rows = "5", @class = "col l12", @required = "required" })
                @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
            </div>
            <div class="col l12 center">
                <div class="g-recaptcha center" data-sitekey="6LdrZjgUAAAAAOvR4ENhn7MlPl5qLczEm3bkgmCN"></div>
                <span class="text-danger">@ViewBag.CaptchaErrorMessage </span>
            </div>

        </div>

        <div class="form-group">
            <div class="col l2"></div>
            <div class="col s12 center">
                <input type="button" value="ثبت نظر" class="btn center btn-default"
                       onclick="Save(@Model.NewsListID,$('#myform').val())" />
            </div>
        </div>
    </div>
}

 و این هم کد :

 public ActionResult SaveComment(int pageID,string comment,FormCollection form)
        {    
 
            string urlToPost = "https://www.google.com/recaptcha/api/siteverify";
            string secretKey = "6LdrZjgUAAAAAGB-Cj-cVyUuxxCaXk9Y8KN8s7IT"; // change this
            string gRecaptchaResponse = this.Request.Form["g-recaptcha-response"];

            var postData = "secret=" + secretKey + "&response=" + gRecaptchaResponse;

            // send post data
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToPost);
            request.Method = "POST";
            request.ContentLength = postData.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(postData);
            }

            // receive the response now
            string result = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
            }

            // validate the response from Google reCaptcha
            var captChaesponse = JsonConvert.DeserializeObject<reCaptchaResponse>(result);
            if (!captChaesponse.Success)
            {
                ViewBag.CaptchaErrorMessage = "Sorry, please validate the reCAPTCHA";
                return View();
            }
}

 اما مشکل اینجاست که this.Request.Form["g-recaptcha-response"] همیشه nullهستش. این رو باید چطور ست کنم؟؟؟

متشکرم

پاسخ های این پرسش

تعداد پاسخ ها : 7 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • دوشنبه 22 آبان 1396
  • 13:56
کاربر سایت

soudabeh

عضویت از 1395/01/12

  • دوشنبه 22 آبان 1396
  • 13:58

من دقیقا از همین تاپیک ها استفاده کردم اما هرچقدر میگردم قسمت g-recaptcha-response رو در هیچ جای فرم پیدا نمیکنم

کاربر سایت

سهیل علیزاده

عضویت از 1396/04/09

  • دوشنبه 22 آبان 1396
  • 14:06

به جای

this.Request.Form["g-recaptcha-response"];

از 

Request["g-recaptcha-response"]

استفاده کنید.

کاربر سایت

soudabeh

عضویت از 1395/01/12

  • دوشنبه 22 آبان 1396
  • 14:07

این یکی هم null هستش

کاربر سایت

سهیل علیزاده

عضویت از 1396/04/09

  • دوشنبه 22 آبان 1396
  • 14:11

مشکلی در کدهای شما دیده نمی شود احتمالا خطایی هنگام دریافت اطالعات از google رخ داده است ، کنسول مرورگر خود را بررسی کنید، پس از همچنین مطمعن شوید که sitekey صحیح می باشد.

کاربر سایت

kaveh.norozi

عضویت از 1394/11/30

  • دوشنبه 22 آبان 1396
  • 18:00

تنظیمات web.config رئ انجام دادی؟

این مراحل رو باید طی کنی تا اوکی بشه:

1) اولا برو سایت گوگل و ثبت نام کن

https://www.google.com/recaptcha

2) کد های زیر رو که از سایت بالا گرفتی تو وب کانفیگ اوکی کن تو قسمت app seting

   <add key="recaptchaPublicKey" value="6LfrHCL6t7pTuP" />
    <add key="recaptchaPrivateKey" value="6LfrHC8UKD0h4sN69q" />
  </appSettings>

3) این مسیر رو در view خودم آدرس دهی کن

<script src='https://www.google.com/recaptcha/api.js'></script>

4) این خطوط رو جایی که میخای کنترل کپچا انجام بشه اضافه کن: مثلا زیر ایمیل برای ارسال مجدد ایمیل

 <div class="g-recaptcha" data-sitekey=@System.Configuration.ConfigurationManager.AppSettings["recaptchaPublicKey"]></div>

5) اینو تو controler خودت اضافه کن

  public static bool ValidateCaptcha(string response)
        {
            //secret that was generated in key value pair  
            string secret = WebConfigurationManager.AppSettings["recaptchaPrivateKey"];

            var client = new WebClient();
            var reply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));

            var captchaResponse = JsonConvert.DeserializeObject<CaptchaResponse>(reply);

            return Convert.ToBoolean(captchaResponse.Success);

        }

6) حالا در controler خودت کنترل کن که آیا وارد شده یا نه

 bool isCapthcaValid = ValidateCaptcha(Request["g-recaptcha-response"]);
            try
            {
                if (isCapthcaValid)
                {
                    if (ModelState.IsValid)
                    {

                      // هر کاری میخای فرمت انجام بده اینجا بزن
                    }
                }
                else
                {
             
                }
            }
            catch (Exception)
            {

                throw;
            }
            

کاربر سایت

soudabeh

عضویت از 1395/01/12

  • سه شنبه 23 آبان 1396
  • 08:24

مشکلش به این دلیل بود که من از input button استفاده کرده بودم و متد رو از طریق ajax فراخوانی میکردم. در صورتی که می بایست از submit استفاده بشه.

از راه حل های همگی متشکرم.

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)