简体   繁体   中英

Google reCAPTACHA - hosted in IIS - Server responded with a status of 503 (Service Unavailable) - MVC5

I integrate Google reCAPTCHA in my Registration page using MVC - Ajax.

I can able to submit reCAPTACHA in my localhost. But after I deployed into the server - getting Service unavailable 503 error. I have a different key for localhost and demo.com

No changes in my code after deployed. I just updated my Google reCAPTACHA Key from config. When Form Submit action getting service unavailable error.

Here is my Google reCaptacha Key 在此处输入图片说明

Here is my index.cshtml page code

<form id="reg-form" class="col-sm-6 col-sm-offset-3" action="@Url.Action("Submit","Registration")" method="post" novalidate>
            <div class="form-group">
                <label class="control-label" for="reg-name">Enter your name:</label>
                <div class="regs">
                    <input id="FullName" name="FullName" class="form-control" type="text" data-error-empty="Please enter your name">
                    <i class="fa fa-user"></i>
                </div>
            </div><!-- End Name input -->
            <div class="form-group">
                <div id='recaptcha' class="g-recaptcha" data-error-empty="The captcha field is required."
                     data-sitekey="publicKeyfromGooglereCAPTACHA"></div>

            </div>
    <p>
        <button name="submit" type="submit" class="btn btn-qubico btn-block g-recaptcha"
                data-error-message="Fill out all information above!" data-sending-message="Sending..." data-ok-message="Message Sent"
                data-callback="onSubmit">
            <i class=" fa fa-location-arrow">
            </i>Submit
        </button>
    </p>  </form>

Here is my Registration.cs Controller

    [HttpPost]
    [System.Web.Services.WebMethod]
    public JsonResult Submit(FormCollection formcollection)
    {
        try
        {
            int status = 0;
            form.FullName = formcollection["FullName"];

            CaptchaResponse response = ValidateCaptcha(Request["g-recaptcha-response"]);

            if (response.Success)
            {
                  status = 1;
            }
            else
            {
                 status = 0;
            }
            return Json(status);
        }
        catch (Exception ex)
        {
            return Json(ex.Message + ex.InnerException.Message);
        }
    }

    [HttpPost]
    /// <summary>  
    /// Validate Captcha  
    /// </summary>  
    /// <param name="response"></param>  
    /// <returns></returns>  
    public static CaptchaResponse ValidateCaptcha(string response)
    {
        string secret = System.Web.Configuration.WebConfigurationManager.AppSettings["recaptchaPrivateKey"];
        var client = new WebClient();
        var jsonResult = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));
        return JsonConvert.DeserializeObject<CaptchaResponse>(jsonResult.ToString());
    } 

Here is my Model CaptchaResponse.cs -

 public class CaptchaResponse
    {
        [JsonProperty("success")]
        public bool Success
        {
            get;
            set;
        }
        [JsonProperty("error-codes")]
        public List<string> ErrorMessage
        {
            get;
            set;
        }  
    }

Here is my Jquery script

$(document).ready(function () {
    $('#reg-form').submit(function () {
         $.ajax({
                type: "POST",
                url: "Registration/Submit",
                data: formInput,
                dataType: "json",
                success: function (data) {
                    console.log(JSON.stringify(data));
                },
                error: function (data) {
                    console.log(JSON.stringify(data));
                }
            });
        }

        return false;
    });

});

Here is my web.config code

 <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="reCaptchaPublicKey" value="publicKeyfromGooglereCAPTACHA" />
    <add key="reCaptchaPrivateKey" value="privateKeyfromGooglereCAPTACHA" />

Note : Application hosted in https port.

Let me know if any more details required.

This issue is fixed. I have added below lines in the config file.

  <handlers>
        <add name="MSCaptcha" verb="GET" path="/CaptchaImage.axd" type="MSCaptcha.captchaImageHandler, MSCaptcha"/>
    </handlers>

or use the below steps to add handlers

  • Select your application in IIS.
  • Open Handler Mappings under IIS either by double click the Handler Mappings or click "open feature" under Action
  • Click "Add Managed Handler" under action.
  • Enter "CaptchaImage.axd" without quotes in Request Path Under Type select MSCaptcha.CaptchaImageHandler
  • Enter "MSCaptcha" without quotes in Name and click OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM