为什么ReCaptcha显示“无效密钥”错误?

时间:2019-01-10 20:24:39

标签: c# asp.net visual-studio-2017

当我的页面加载时,ReCapthca窗口小部件立即显示“无效键”错误。我检查了密钥,甚至创建了新密钥,然后将它们复制并粘贴到我的网站解决方案中。当我从本地运行项目时,没有错误出现。从开发服务器运行它时,出现错误。我不知道这是否重要,但是我在整个项目中使用了我认为允许的相同密钥。另外,以前不是问题,但现在是。

根据ReCaptcha文档,这意味着密钥不再有效,我需要创建新密钥。我这样做了,然后将它们放在我的代码中(复制并粘贴,这样我就不会弄错它们)。然后,我检查了密钥开头和结尾的空格,因为以前我注意到复制密钥时有时会添加空格。这里没有运气。

我还从其他页面上取下了验证码,以查看是否在不同页面上使用了相同的键。那里也没有运气。

我对按键进行了两次和三次检查,没有任何毛病。

<%--RECAPTCHA WIDGET V2-CHECKBOX--%>
<div class="g-recaptcha" data-sitekey="MyCheckboxSiteKey"></div>
<br />

<%--RECAPTCHA WIDGET V2-INVISIBLE--%>
<div id="recaptcha class=g-recaptcha" data-sitekey="MyInvisibleSiteKey" data-callback="BtnSubmit" data-size="invisible"></div>


private void RunV2Call()
{
    //Getting the Checkbox Server Side Response
    var sbi = new StringBuilder();
    sbi.Append("https://www.google.com/recaptcha/api/siteverify?secret=");

    //----  OUR RECAPTCHA WIDGET V2-INVISIBLE SECRET KEY
    var secretKey = "MyCheckboxSecretKey";
    sbi.Append(secretKey);

    //----  RESPONSE FROM RECAPTCHA CONTROL
    sbi.Append("&");
    sbi.Append("response=");
    var reCaptchaResponse = Request["g-recaptcha-response"];
    sbi.Append(reCaptchaResponse);

    //CLIENT IP ADDRESS
    //----  THIS IP ADDRESS PART IS OPTIONAL. 
    //----  IF YOU DO NOT WANT TO SEND IP ADDRESS YOU CAN
    //----  SKIP OR COMMENT OUT OR REMOVE BELOW 4 LINES
    //----  ALSO COMMENT OUT OR REMOVE THE METHOD 'GetUserIp()'
    sbi.Append("&");
    sbi.Append("remoteip=");
    var clientIpAddress = GetUserIp();
    sbi.Append(clientIpAddress);

    //----  MAKE THE API CALL AND DETERMINE VALIDITY
    using (var client = new WebClient())
    {
        var uri = sbi.ToString();
        var json = client.DownloadString(uri);
        var serializer = new DataContractJsonSerializer(typeof(RecaptchaApiResponse));
        var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        var result = serializer.ReadObject(ms) as RecaptchaApiResponse;

        //--- CHECK IF WE ARE ABLE TO CALL API OR NOT
        if (result == null)
        {
            lblResult.Text = "";
            lblResult.Text = "Captcha was unable to make the api call";
        }
        else //---- IF YES
        {
            //----  IF API CALL CONTAINS ERRORS
            if (result.ErrorCodes != null)
            {
                if (result.ErrorCodes.Count > 0)
                {
                     foreach (var error in result.ErrorCodes)
                     {
                         lblResult.Text = "";
                         lblResult.Text = "reCAPTCHA Error: " + error;
                         lblResult.ForeColor = System.Drawing.Color.Red;
                     }
                 }
             }
             else //----    IF API DOES NOT CONTAIN ERRORS
             {
                 if (!result.Success) //----    IF CAPTCHA WAS UNSUCCESSFUL FOR SOME REASON
                 {
                     lblResult.Text = "";
                     lblResult.Text = "Captcha did not pass, please try again.";
                     lblResult.ForeColor = System.Drawing.Color.Red;
                 }
                 else //---- IF SUCCESSFULLY VERFIED; DO THE REST OF YOUR LOGIC
                 {
                     lblResult.Text = "";
                     lblResult.Text = "Success";
                     lblResult.ForeColor = System.Drawing.Color.Green;

                     ClearInputs();
                 }
             }
         }
    }
}

private void RunViCall()
{
    var sbi = new StringBuilder();
    sbi.Append("https://www.google.com/recaptcha/api/siteverify?secret=");

    //----  OUR RECAPTCHA WIDGET V2-INVISIBLE SECRET KEY
    var secretKey = "MyInvisibleSecretKey";
    sbi.Append(secretKey);

    //----  RESPONSE FROM RECAPTCHA CONTROL
    sbi.Append("&");
    sbi.Append("response=");
    var reCaptchaResponse = Request["g-recaptcha-response"];
    sbi.Append(reCaptchaResponse);

    //CLIENT IP ADDRESS
    //----  THIS IP ADDRESS PART IS OPTIONAL. 
    //----  IF YOU DO NOT WANT TO SEND IP ADDRESS YOU CAN
    //----  SKIP OR COMMENT OUT OR REMOVE BELOW 4 LINES
    //----  ALSO COMMENT OUT OR REMOVE THE METHOD 'GetUserIp()'
    sbi.Append("&");
    sbi.Append("remoteip=");
    var clientIpAddress = GetUserIp();
    sbi.Append(clientIpAddress);

    //----  MAKE THE API CALL AND DETERMINE VALIDITY
    using (var client = new WebClient())
    {
        var uri = sbi.ToString();
        var json = client.DownloadString(uri);
        var serializer = new DataContractJsonSerializer(typeof(RecaptchaApiResponse));
        var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
        var result = serializer.ReadObject(ms) as RecaptchaApiResponse;

        //--- CHECK IF WE ARE ABLE TO CALL API OR NOT
        if (result == null)
        {
            lblResult.Text = "";
            lblResult.Text = "Captcha was unable to make the api call";
        }
        else //---- IF YES
        {
            //----  IF API CALL CONTAINS ERRORS
            if (result.ErrorCodes != null)
            {
                if (result.ErrorCodes.Count > 0)
                {
                     foreach (var error in result.ErrorCodes)
                     {
                         lblResult.Text = "";
                         lblResult.Text = "reCAPTCHA Error: " + error;
                         lblResult.ForeColor = System.Drawing.Color.Red;
                     }
                 }
             }
             else //----    IF API DOES NOT CONTAIN ERRORS
             {
                 if (!result.Success) //----    IF CAPTCHA WAS UNSUCCESSFUL FOR SOME REASON
                 {
                     lblResult.Text = "";
                     lblResult.Text = "Captcha did not pass, please try again.";
                     lblResult.ForeColor = System.Drawing.Color.Red;
                 }
                 else //---- IF SUCCESSFULLY VERFIED; DO THE REST OF YOUR LOGIC
                 {
                     lblResult.Text = "";
                     lblResult.Text = "Success";
                     lblResult.ForeColor = System.Drawing.Color.Green;

                     ClearInputs();
                 }
             }
         }
    }
}

private object GetUserIp()
{
    var visitorsIpAddr = string.Empty;

    if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        visitorsIpAddr = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    }
    else if (!string.IsNullOrEmpty(Request.UserHostAddress))
    {
        visitorsIpAddr = Request.UserHostAddress;
    }
    return visitorsIpAddr;
}

[DataContract]
public class RecaptchaApiResponse
{
    //STRUCT TO CAPTURE RECAPTCHA RESPONSE
    [DataMember(Name = "success")]
    Public bool Success;
    [DataMember(Name = "error-codes")]
    public List<string> ErrorCodes;
}

以前,此代码工作正常。几个月以来,我没有碰过它,因为我有更多的网站要开发。我回到这段代码是因为我正在开发另一种需要启用ReCaptcha的表单。我希望它能继续工作直到V2被弃用

0 个答案:

没有答案